Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set an asynchronous validation when I use superRefine method in zod #1

Open
EmanuelCav opened this issue Mar 17, 2024 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@EmanuelCav
Copy link
Owner

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"]
        })
    }

})
@EmanuelCav EmanuelCav added the help wanted Extra attention is needed label Mar 17, 2024
@EmanuelCav 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant