-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
discriminatedUnion
errors with z.object().transform()
#1477
Comments
What exactly are you trying to achieve? Discriminating types usually result in several different types. In your case they are both of the same one: {
type: 'foo' | 'bar',
value: string;
} If the main purpose of the transformations are further manipulations/refinements, will this be suitable for your needs: const ZodFoo = zod.object({type: zod.literal('foo'), foo: zod.string()});
const ZodBar = zod.object({type: zod.literal('bar'), bar: zod.string()});
const ZodBaz = zod.discriminatedUnion('type', [ZodFoo, ZodBar])
.transform(obj => ({
type: obj.type,
value: obj.type === 'foo' ? obj.foo : obj.bar,
})); ? |
@azhiv thanks for your feedback. I tried to reduce the example to a minimum for the sake of simplicity. Yet I did not succeed ;) Let me try again: const ZodFoo = z.object({type: z.literal('foo'), foo: z.string()});
const ZodBar = z.object({type: z.literal('bar'), bar: z.string().optional(), fallback: z.string()})
.transform(({type, bar, fallback}) => ({type, bar: bar || fallback}));
const ZodBaz = z.discriminatedUnion('type', [ZodFoo, ZodBar]); |
Ok, I couldn't find a way to combine |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Playground
I am not sure if this is even possible.
If not, is there a recommended workaround?
The text was updated successfully, but these errors were encountered: