Skip to content
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

Tuples in unions does not get validated since v3.9.1 #668

Closed
screendriver opened this issue Sep 27, 2021 · 9 comments
Closed

Tuples in unions does not get validated since v3.9.1 #668

screendriver opened this issue Sep 27, 2021 · 9 comments
Labels
wontfix This will not be worked on

Comments

@screendriver
Copy link

Following schema

const eventsSchema = z.union([
    z.object({
      eventName: z.literal('foo'),
      eventPayload: z.tuple([z.number().nonnegative(), z.number().nonnegative()])
    }).strict(),
    z.object({
      eventName: z.literal('bar'),
      eventPayload: z.string().email()
    }).strict()
]);

failed before v3.9.1 with following data

const data = {
  "eventName": "foo",
  "eventPayload": [0, -60]
};
const parseResult = eventsSchema.safeParse(data);
// parseResult.success === true

but since v3.9.1 zod does not validate the -60 value anymore as .nonnegative(). Parsing is always successful whereas before v3.9.1 it failed (which is the expected behaviour)

@anguskeatinge
Copy link

anguskeatinge commented Sep 27, 2021

I have a similar issue, the email part of the validation is broken for both union and or

I only just moved from v3.4.1 to v3.9.1 so the regression was introduced somewhere in there. (just checked, v3.9.0 works fine)

Here are some examples:

const BrokenSchema = z.union([
    z.object({
        email: z.string().email(),
    }),
    z.object({
        email: z.literal(null),
    }),
]);
// same as above
const BrokenSchema2 = z
    .object({
        email: z.string().email(),
    })
    .or(
        z.object({
            email: z.literal(null),
        }),
    );

const res1 = BrokenSchema.safeParse({ email: 'asdf' });
expect(res1.success).toBe(false); // this test DOES NOT pass

const res2 = BrokenSchema.safeParse({ email: 9 });
expect(res2.success).toBe(false); // this test DOES pass

@colinhacks
Copy link
Owner

I'll look into this today.

@oleg-codaio
Copy link

Here's another repro that fails (as expected) in 3.8.2 and succeeds in 3.9.0:

const schema = z.union([z.string().refine(() => false), z.number().refine(() => false)]);
console.log('result is', schema.parse('asdf'));

@colinhacks
Copy link
Owner

Should be fixed in 3.9.2+

@ohana54
Copy link

ohana54 commented Sep 29, 2021

Thanks @colinhacks, for us the case that @oleg-codaio presented still doesn't work (in 3.9.4).
Simplified example: https://codesandbox.io/s/eager-yonath-fb3mq

z.union([z.number(), z.string().refine(() => false)]).parse("a");

@alax
Copy link

alax commented Oct 1, 2021

Yes, running v3.9.5, this is an issue for us at Deletype as well.

It seems any union that includes a string with a refiner attached (like ohana54's example) just doesn't work. Here is what is failing for us:

z.union([
    z.number(),
    z.string().refine(val => !isNaN(Number(val)))
])

@colinhacks
Copy link
Owner

Oops looks like I failed to fully roll back the changes that caused this error. I've confirmed that all the above cases are working in Zod v3.9.8+.

@oleg-codaio
Copy link

oleg-codaio commented Oct 6, 2021

Thanks! Hopefully there are unit tests too to help catch any future regressions. (happy to help contribute some if not quite yet.)

@stale
Copy link

stale bot commented Mar 2, 2022

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.

@stale stale bot added the wontfix This will not be worked on label Mar 2, 2022
@stale stale bot closed this as completed Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

6 participants