I am using zod to verify a ’email’ and ‘confirmemail’ inputs from a React (v18.3) form.
Inputs are transmitted to a server component for verification. (NextJS v14.2)
This is the schema (in server component)
const emailValidationSchema = z.object({
email: z.string().email(),
confirmEmail: z.string().email()
})
.refine(data => data.email === data.confirmEmail, {
message: 'Emails must match.',
path: ['email', 'confirmEmail'],
})
This is the formData (in server component)
const email = formData.get("email") as string
const confirmemail = formData.get("confirmemail") as string
const verifyemailobject = { email: email as string, confirmemail: confirmemail as string }
Zod parse called here (in server component)
try {
console.log('email: ', email, ' verify: ', confirmemail)
emailValidationSchema.parse(verifyemailobject)
} catch (error) {
console.error('Zod email error: ', error)
}
console shows
email: [email protected] verify: [email protected]
Zod email error: ZodError: [
{
"code": "invalid_type",
"expected": "string",
"received": "undefined",
"path": [
"confirmEmail"
],
"message": "Required"
}
]
The emails are entered as strings in the client component, passed to the server component as strings, reconfirmed in the verifyemailobject as strings, but zod ‘says’ they are undefined.
a cut down version of the schema produces the same error
const emailValidationSchema = z.object({
email: z.string().email(),
confirmEmail: z.string().email()
})
1