You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the example above, I first entered something in passport, then switched to driver_license, but when I submit the form, the value of passport is there as well.
I know this is expected since I defined it in the schema, just want to know whether there's a way to get around?
The discarding totally satisfies the schema, since both passport and driver_license is optional, hence discarding them won't break the type.
I'm aware that I can use z.discriminatedUnion() for this particular case, but it's not really my preferred solution, as in the real-world case, the complexity of the form is ridiculous, where usage of Discriminated Union will only make the schema even harder to organize in my opinion.
And I want the interface to looks like a simple single one instead of complex intersection with discriminated union
// This is what I needinterfaceUser{name: string;age: number;id_type: 'passport'|'driver_license'driver_license?: string;passport?: string;}// Instead oftypeUser={name: string;age: number;}&({id_type: 'passport';passport: string;}|{id_type: 'driver_license'driver_license: string;})// OrtypeUser={id_type: 'passport';name: string;age: number;passport: string;}|{id_type: 'driver_license';name: string;age: number;driver_license: string;}
In yup, I can still do transform after when condition (e.g. transform to undefined), can I do something similar like conditionally discard a field in .refine() or .superRefine() in zod? I currently can't find a way to do so.
Or, as another solution, is it possible to make a new feature that removes all falsy value or any specified value during parse? e.g. Automatically remove empty string ''
The text was updated successfully, but these errors were encountered:
Re-use the example in #2524:
https://stackblitz.com/edit/vitejs-vite-c6cz55?file=src%2FApp.tsx
In the example above, I first entered something in
passport
, then switched todriver_license
, but when I submit the form, the value ofpassport
is there as well.I know this is expected since I defined it in the schema, just want to know whether there's a way to get around?
The discarding totally satisfies the schema, since both
passport
anddriver_license
isoptional
, hence discarding them won't break the type.I'm aware that I can use
z.discriminatedUnion()
for this particular case, but it's not really my preferred solution, as in the real-world case, the complexity of the form is ridiculous, where usage ofDiscriminated Union
will only make the schema even harder to organize in my opinion.And I want the interface to looks like a simple single one instead of complex
intersection
withdiscriminated union
In
yup
, I can still dotransform
afterwhen
condition (e.g. transform toundefined
), can I do something similar like conditionally discard a field in.refine()
or.superRefine()
inzod
? I currently can't find a way to do so.Or, as another solution, is it possible to make a new feature that removes all
falsy
value or any specified value during parse? e.g. Automatically remove empty string''
The text was updated successfully, but these errors were encountered: