Check out our talk at Remix Conf!

Zod Effects

In this example, we use Zod refine and superRefine to make our schemas more powerful.

const schema = z
  .object({
    planType: z.enum(['personal', 'corporate']),
    quantity: z.number().min(0),
  })
  .refine(
    ({ planType, quantity }) => !(planType === 'personal' && quantity >= 8),
    {
      message: 'For 8 cards or more please use our corporate plan',
      path: ['planType'],
    },
  )
  .superRefine((arg, ctx) => {
    const isCorporate = arg.planType === 'corporate'

    if (isCorporate && arg.quantity < 8) {
      ctx.addIssue({
        code: z.ZodIssueCode.too_small,
        minimum: 8,
        message: 'For corporate cards you must issue at least 8',
        type: 'number',
        inclusive: true,
        fatal: true,
        path: ['planType'],
      })
    }
  })

const mutation = makeDomainFunction(schema)(async (values) => values)

export const action: ActionFunction = async ({ request }) =>
  formAction({ request, schema, mutation })

export default () => <Form schema={schema} />