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

Zod Async Refine always returning false #3519

Closed
nosizejosh opened this issue May 23, 2024 · 1 comment
Closed

Zod Async Refine always returning false #3519

nosizejosh opened this issue May 23, 2024 · 1 comment

Comments

@nosizejosh
Copy link

Hello,

I am trying to validate a form with an async request to a server route that works fine when tested on its own, however, in the zod validation always returns false.

I am trying to prevent user from attempting to register with an email that has already been taken, and so validating email inside a nuxtui form by calling this async server route.

testing the server route works just fine and return expected true or false
http://localhost:3000/user/is_email_available?email=er.ttt@gmail.com returns true or false as expected
but for some reason always returns false in the zod schema validation.

const schema = z.object({
  tenant_name: z.string({ required_error: "Business Name is required" }).
    min(2, { message: "Must be at least 2 characters" }).
    refine(async (val) => await tenantSlugAvailable(slugify(val)), (val) => ({ message: `${val} is not available` })),
  email: z.string({ required_error: "Business Email is required" }).email({ message: "Invalid email address" }).
    refine(async (val) => { await useFetch('user/is_email_available', { query: { email: val } }) }, (val) => ({ message: `${val} is not available` })),
  password: z.string({ required_error: "Password is required" }).min(8, { message: 'Must be at least 8 characters' }),
  confirmPassword: z.string({ required_error: "Password Confirmation is required" }).min(8, { message: 'Must be at least 8 characters' })
}).refine((values) => values.password === values.confirmPassword,
  {
    message: "Passwords must match!",
    path: ["confirmPassword"],
  }
);

Please can someone take a look and show me what I am doing wrong? have spent a day on this already and not making any headway.

Thanks

@nosizejosh
Copy link
Author

nosizejosh commented May 24, 2024

resolved with the help of @Boluwatife2904.
Issue was with returning a value from the usefetch call.

email: z.string({ required_error: "Business Email is required" }).email({ message: "Invalid email address" }). refine(async (val) => { const { data, error } = await useFetch('/user/is_email_available', { query: { email: val } }); return data.value }, (val) => ({ message:${val} is not available })),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant