You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use an asynchronous validation on superRefine in zod but I got this error: Async refinement encountered during synchronous parse operation. Use .parseAsync instead.
I'm trying to register an user. I'm using prisma as a database access
import { prisma } from '../../../helper/prisma'
import { z } from 'zod'
export const registerSchema = z.object({
username: z.string().min(3, {
message: "Username must have at least 3 characters"
}).max(35, {
message: "Username must have less than 35 characters"
}).trim().refine((value) => /^[a-zA-Z0-9ñÑ]+$/.test(value), {
message: "Username only accepts letters and numbers continuously"
}),
email: z.string().min(1, {
message: "Email field is required"
}).trim().email({
message: "Email is invalid"
}),
gender: z.string().trim().min(1, {
message: "Gender field is required"
}),
password: z.string().min(7, {
message: "Password must have more than 6 characters"
}).trim(),
confirm: z.string().trim().min(1, {
message: "Confirm password field is required"
})
}).superRefine(async ({ password, confirm, username, email }, ctx) => { // Set asynchronous validation
if (password !== confirm) {
ctx.addIssue({
code: "custom",
message: "The passwords do not match",
path: ["password"]
})
}
const emailExists = await prisma.user.findUnique({ // This does not work
where: {
email
}
})
if (emailExists) {
ctx.addIssue({
code: "custom",
message: "The email already exists",
path: ["email"]
})
}
const usernameExists = await prisma.user.findUnique({ // This does not work
where: {
username
}
})
if (usernameExists) {
ctx.addIssue({
code: "custom",
message: "The username already exists",
path: ["username"]
})
}
})
The text was updated successfully, but these errors were encountered:
EmanuelCav
changed the title
How to set an Asynchronous validation when I use superRefine method in zod
How to set an asynchronous validation when I use superRefine method in zod
Mar 17, 2024
I'm trying to use an asynchronous validation on
superRefine
inzod
but I got this error:Async refinement encountered during synchronous parse operation. Use .parseAsync instead.
I'm trying to register an user. I'm using prisma as a database access
The text was updated successfully, but these errors were encountered: