Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

zod validation in javaScript

export type ResetPassword = {
  password: string
  confirmPassword: string
}

export const resetPasswordSchema: ZodType<ResetPassword> = z.object({
  password: z.string().refine((value) => value !== '', {
    message: 'Password is required'
  }),
  confirmPassword: z.string().refine((value) => value !== '', {
    message: 'Password is required'
  }).refine((data) => data.password === data.confirmPassword, {
    message: "Passwords don't match",
    path: ["confirmPassword"], // path of error
  })
})

i’m trying to use zod validation to check if the confirm password matches the password and i got this error!

Property ‘password’ does not exist on type ‘string’.
Property ‘confirmPassword’ does not exist on type ‘string’.

in this line

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  }).refine((data) => data.password === data.confirmPassword, {

…………………

>Solution :

You will need to put the refine method on the end of the schema to get access to all the properties like this:

export const resetPasswordSchema: ZodType<ResetPassword> = z.object({
  password: z.string().refine((value) => value !== '', {
    message: 'Password is required'
  }),
  confirmPassword: z.string().refine((value) => value !== '', {
    message: 'Password is required'
  })
})
.refine((data) => data.password === data.confirmPassword, {
    message: "Passwords don't match",
    path: ["confirmPassword"], // path of error
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading