8000 Error on nested discriminated unions · Issue #3804 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Error on nested discriminated unions #3804

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

Open
BrianArch96 opened this issue Oct 17, 2024 · 9 comments
Open

Error on nested discriminated unions #3804

BrianArch96 opened this issue Oct 17, 2024 · 9 comments

Comments

@BrianArch96
Copy link
BrianArch96 commented Oct 17, 2024

Hi,

I have a schema like so:

const bananaSchema = z.object({
  type: z.literal('fruit'),
  subType: z.literal('banana'),
  value: z.string(),
});

const orangeSchema = z.object({
  type: z.literal('fruit'),
  subType: z.literal('orange'),
  value: z.string(),
});

const discriminatedFruitSchema = z.discriminatedUnion('subType', [bananaSchema, orangeSchema]);

const vegetableSchema = z.object({
  type: z.literal('vegetable'),
  value: z.string(),
});

export const discriminatedFoodSchema = z.discriminatedUnion('type', [
  vegetableSchema,
  ...discriminatedFruitSchema.options,
]);

All is good at first, but when I parse using the schema I get the following error:

/.../zod/lib/types.js:2386
                    throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
                          ^
Error: Discriminator property type has duplicate value fruit
@BrianArch96 BrianArch96 changed the title Nested discriminated unions are failing to compile Error on nested discriminated unions Oct 17, 2024
@sunnylost
Copy link

bananaSchema and orangeSchema has the same type.

@BrianArch96
Copy link
Author
BrianArch96 commented Oct 17, 2024

bananaSchema and orangeSchema has the same type.

Yes, that's intended, as both are of type fruit. It's their subtypes ('banana' and 'orange') that differ. So I basically want something like:

               type: fruit/vegetable DU
                         |
     orange/banana DU        vegetable schema
          |
  subType: banana/orange
                 |
  orange schema     banana schema

@sunnylost
Copy link

If you combine all of them, you can't use type alone to distinguish between them. What you need is something like this:

z.discriminatedUnion(z.union(['type', 'subType']), [
  vegetableSchema,
  ...discriminatedFruitSchema.options,
]);

However, currently, it only supports a single string as the discriminator.

@BrianArch96
Copy link
Author

Yeah, I'm guessing nested discriminated unions with different discriminators aren't currently supported?

@sunnylost
Copy link

Yes.

@naseer-medibank
Copy link

any update on this?

@iltan987
Copy link
iltan987 commented Apr 1, 2025

It's really needed. Any update?

@BrianArch96
Copy link
Author
BrianArch96 commented Apr 29, 2025

Hi guys, I believe this will be fixed as part of Zod v4.

@iltan987
Copy link
iltan987 commented Apr 30, 2025

Hi guys, I believe this will be fixed as part of Zod v4.

Until now, in beta, is it fixed?
Did anyone try?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
0