Tutorial

Cross-Field Validation Rules

Validate rules that span multiple fields like password confirm, date ranges, and conditional billing with a single refine over the whole form.

On this page

  1. Introduction
  2. Enumerate the rules
  3. Refine the whole form
  4. Target the right field
  5. Conditional requirements
  6. Run on both sides
  7. Test the combos
  8. Sources

Introduction

This guide covers cross-field validation rules that span multiple inputs, a recurring concern in the form-validation scenario. Some rules cannot be expressed per field: a password must match its confirmation, an end date must follow a start date, and a billing address is required only when paying by invoice. These are cross-field rules, and the clean place for them is a single refine that sees the whole form.

Enumerate the rules

Write down each cross-field rule and the fields it reads, so you can implement them in one place and test each combo without hunting through handlers.

The trade-off worth naming is that write down each cross-field rule and the fields it reads, so you can implement them in one place and test each combo without hunting through handlers adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Refine the whole form

Use superRefine to inspect the full object and add issues for each violated rule, which keeps cross-field logic out of per-field validators where it does not belong.

The trade-off worth naming is that use superrefine to inspect the full object and add issues for each violated rule, which keeps cross-field logic out of per-field validators where it does not belong adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

const Form = z.object({
  password: z.string().min(8),
  confirm: z.string(),
  start: z.string(),
  end: z.string(),
}).superRefine((v, ctx) => {
  if (v.password !== v.confirm) ctx.addIssue({ code:'custom', message:'Passwords do not match', path:['confirm'] });
  if (v.end < v.start) ctx.addIssue({ code:'custom', message:'End before start', path:['end'] });
});

Target the right field

Set the issue path to the field the user should edit, so the form highlights the confirmation input rather than a generic error at the top of the page.

The trade-off worth naming is that set the issue path to the field the user should edit, so the form highlights the confirmation input rather than a generic error at the top of the page adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Conditional requirements

Make a field required only when another field has a certain value by checking the combo in refine, so the rule lives in the schema and not in scattered UI logic.

The trade-off worth naming is that make a field required only when another field has a certain value by checking the combo in refine, so the rule lives in the schema and not in scattered ui logic adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

const Pay = z.object({
  method: z.enum(['card','invoice']),
  billing: z.object({ addr: z.string() }).optional(),
}).superRefine((v, ctx) => {
  if (v.method === 'invoice' && !v.billing?.addr) {
    ctx.addIssue({ code:'custom', message:'Billing address required for invoice', path:['billing','addr'] });
  }
});

Run on both sides

Share the schema between client and server so the same cross-field rules run before submit and on the boundary, which removes a class of inconsistent feedback.

The trade-off worth naming is that share the schema between client and server so the same cross-field rules run before submit and on the boundary, which removes a class of inconsistent feedback adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test the combos

For each rule, assert the passing combo resolves valid and each failing combo produces the expected field error, so a refactor cannot silently drop a rule.

The trade-off worth naming is that for each rule, assert the passing combo resolves valid and each failing combo produces the expected field error, so a refactor cannot silently drop a rule adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: Zod refine, Yup mixed test.

FAQ

Where should a cross-field rule live, on the schema or the form?
On the schema, so the same rule runs on the server and the client; putting it only in the form component duplicates the logic and lets the server accept invalid combos.
How do I point the error at the right field?
Use a refine that calls addIssue with a path array naming the field the user should edit, so the form can highlight that input instead of a generic form-wide error.

Related Examples