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
}).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
})