Zod v4 Should Support Field-Level Cross Validation Without Workarounds #5781
-
|
The Problem One of the most common validation patterns in real-world apps is cross-field validation — things like "confirm password must match password" or "end date must be This has left a gap that's hitting a lot of developers, especially those using form libraries like React Hook Form, where field-level validation is the standard Currently the workarounds are: Moving validation to the parent object level — but this breaks form-per-field error mapping A first-class API for accessing sibling field values during field-level refinement. Something like: const schema = z.object({ Or alternatively, a dependsOn modifier: const schema = z.object({ Why This Matters Password confirmation, date ranges, conditional required fields — these aren't edge cases, they're everyday patterns |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hii @BittuChan, thanks for writing it up so clearly. I ran into this exact problem migrating a Next.js app from v3 to v4. I had cross-field validation everywhere password confirmations, date range pickers, conditional required fields based on a toggle and all of it broke overnight when ctx.parent was removed. What I tried (and why each fell short):
Why I think the dependsOn API is the right direction:
That said, I'd add one thing to the proposal which is support for depending on multiple fields: const schema = z.object({ |
Beta Was this translation helpful? Give feedback.
Hii @BittuChan, thanks for writing it up so clearly. I ran into this exact problem migrating a Next.js app from v3 to v4. I had cross-field validation everywhere password confirmations, date range pickers, conditional required fields based on a toggle and all of it broke overnight when ctx.parent was removed.
What I tried (and why each fell short):