Dev
TypeScript
zod
Validations

Zod Validations

Some things to know with refine and superRefine

  • Checkings in refine and superRefine 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 of ID_CARD or PASSPORT. It will not run if identity_type is still empty or undefined (invalid_type error).