Feature request: one of fields required
See original GitHub issueIf I am not mistaken right now you have to use refine
and write your own logic to support such a feature. Zods goal is a nice developer experience and I think this would a good addition.
const schema = z.object({
name: z.string().nonempty(),
familyName: z.string().nonempty(),
email: z.string().email(),
phone: z.string(),
}).oneOf('email', 'phone')
This schema would only require nonempty email
or phone
.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Feature Request: Required Fields for forms
So I created a form with some basic fields from a table. The form then has a button to submit the data and...
Read more >How To Manage Feature Requests [Template included]
This guide will teach you everything you need to know about feature requests – how to process them, manage them, respond to them,...
Read more >Feature Requests: How to Collect Them and Engage Users in ...
To do so, you should have one central location for collecting feedback from users. "We listen to our customers' feedback about what features...
Read more >Feature Request Content - Instabug
When submitting a new feature request, users must fill out four fields. ... By default, a user's email address is required to submit...
Read more >Feature Request: Add ability to define required/optional fields ...
I know this can be specified in the description, but it is not ideal. :) Here are the columns I am proposing for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If I understand correctly, one downside of the refinement is that it doesn’t carry through to the type system, so TS won’t understand that exactly one of these keys has a value.
I know in TS you can express this “exactly one” requirement like:
(Which correctly requires exactly one of those two keys, lets you destructure, types
result.email ?? result.phone
asstring
instead ofstring | undefined
, etc.)Looks like you can more or less directly translate this to zod (which is awesome!):
Not sure if there’s a more elegant way to do this, but it seems to be working. Also, I’m pretty sure this could be adapted to support “at least one of a or b” instead of “exactly one”.
Just thought I’d share in case anyone else comes across this issue trying to figure out how to get this working like I did.
I think this is a nice idea but too niche to introduce into core. If people disagree feel free to comment and perhaps I’ll revisit this.
It’s easy enough to split out this logic in a reusable way.