Tutorial

How to Validate a Date Range Cross Field

Validate that a start date is before an end date using a cross-field schema, so the rule lives in the schema and the UI can show the error on either field.

On this page

  1. Introduction
  2. Define the range schema
  3. Use superRefine for the rule
  4. Attach the error to a field
  5. Handle equal dates
  6. Test boundary cases
  7. Show error on both fields
  8. Common pitfalls
  9. Conclusion

Introduction

The hardest part of validate start before end is not writing the code — it is deciding what the code is allowed to reject. A Zod cross-field setup that accepts everything is easy to ship and expensive to operate, because every malformed input becomes a production surprise. The discipline is to enumerate the shapes the system actually receives, encode them in Zod cross-field, and let the library reject the rest loudly. This keeps the happy path fast and the failure path visible.

Zod cross-field earns its place when validate start before end is the bottleneck the user feels. Before that, it is overhead that slows the common case to protect a rare one. Measure the rejection rate and the tail latency, then decide whether the guard is worth its cost. A guard that never fires is a guard you can remove; a guard that fires constantly is a signal that the producer contract is wrong, not that the guard is too strict.

The sections below walk through the concrete decisions: where to put the zod cross-field check, how to keep it fast under load, what to do when the contract changes, and how to test the failure paths so they degrade on purpose instead of by accident.

Define the range schema

Define the range schema is the first decision because it determines what the rest of the Zod cross-field pipeline can assume. A wrong choice here turns into validate start before end under load, and the invariant you thought Zod cross-field guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Define the range schema is the one most teams skip: drive zod cross-field to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

const Range = z.object({ start: z.coerce.date(), end: z.coerce.date() })
  .superRefine((d, ctx) => {
    if (d.start >= d.end) ctx.addIssue({
      code: 'custom', path: ['end'], message: 'end must be after start',
    });
  });

The typescript above is the shape to ship for validate start before end. It is small enough to audit and specific enough that the failure behavior is obvious. Adapt the constants to your traffic and latency budget, and resist the urge to handle cases the system has not seen yet.

Use superRefine for the rule

The mechanism behind Use superRefine for the rule is straightforward in Zod cross-field, but the consequences ripple. A wrong choice here turns into validate start before end under load, and the invariant you thought Zod cross-field guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Use superRefine for the rule is the one most teams skip: drive zod cross-field to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Attach the error to a field

Attach the error to a field is where most Zod cross-field implementations leak performance. A wrong choice here turns into validate start before end under load, and the invariant you thought Zod cross-field guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Attach the error to a field is the one most teams skip: drive zod cross-field to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

test('rejects reversed range', () => {
  expect(Range.safeParse({ start: '2026-02-01', end: '2026-01-01' }).success).toBe(false);
});

Pin the behavior with this test. A Zod cross-field setup that passes this test degrades on purpose instead of by accident, which is the difference between a managed failure and an incident.

Handle equal dates

The hard part of Handle equal dates is not the happy path — it is what happens when zod cross-field fails halfway. A wrong choice here turns into validate start before end under load, and the invariant you thought Zod cross-field guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Handle equal dates is the one most teams skip: drive zod cross-field to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Test boundary cases

Test boundary cases should be observable without a debugger. A wrong choice here turns into validate start before end under load, and the invariant you thought Zod cross-field guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Test boundary cases is the one most teams skip: drive zod cross-field to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Show error on both fields

Show error on both fields is a trade, not a winner. A wrong choice here turns into validate start before end under load, and the invariant you thought Zod cross-field guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Show error on both fields is the one most teams skip: drive zod cross-field to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Common pitfalls

The common pitfalls in validate start before end all reduce to one mistake: treating Zod cross-field as a formality instead of a contract. The contract is the thing the rest of the system relies on, and a weak contract propagates weakness everywhere.

  • Skipping the zod cross-field boundary so validate start before end enters unvalidated.
  • Assuming Zod cross-field is fast without measuring the tail latency under load.
  • Letting validate start before end fail silently instead of failing loudly with a metric.
  • Testing only the happy path so the zod cross-field failure surfaces in production.
  • Sharing mutable state across requests so one bad input poisons the next.

Conclusion

Do not conflate the conclusion with the implementation. They look similar from the outside but the failure modes differ: Zod cross-field fails fast and the caller knows, while an unchecked path fails slow and the caller guesses. Choose the one whose failure mode your system can absorb, and document the choice at the call site.

Self-check: write a test that drives zod cross-field to the failing condition and assert the system degrades gracefully. Then validate start before end is a property you maintain, not a feature you hope for.

Sources: Zod superRefine

FAQ

Why path end and not start?
The error attaches to the field the user is most likely to fix. Attach to both if the UI supports it.
Should equal dates pass?
Decide the business rule. Use >= to reject equality or > to allow it.

Related Examples