Zod Validations
Some things to know with refine
and superRefine
-
Checkings in
refine
andsuperRefine
will only run only if there are not invalid type error anymore on the base schema level.// example import { z } from 'zod' const formSchema = z .object({ name: z.string(), identity_type: z.enum(['ID_CARD', 'PASSPORT']), is_married: z.boolean(), spouse_name: z.string(), }) .refine((data) => data.is_married && data.spouse_name, { path: ['spouse_name'], message: 'Spouse Name must be filled', })
The refine checking will only run if
identity_type
is one ofID_CARD
orPASSPORT
. It will not run ifidentity_type
is still empty or undefined (invalid_type error).