question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tuples in unions does not get validated since v3.9.1

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
colinhackscommented, Sep 28, 2021

Should be fixed in 3.9.2+

3reactions
colinhackscommented, Oct 5, 2021

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+.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type manipulations: union to tuple · Issue #13298 - GitHub
A suggestion to create a runtime array of union members was deemed out of scope because it would not leave the type system...
Read more >
5. Data Structures — Python 3.11.1 documentation
Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly...
Read more >
Ecto.Changeset — Ecto v3.9.4 - HexDocs
If the changes are valid, all changes are applied to the changeset data. If the changes are invalid, no changes are applied, and...
Read more >
Schema Validation - python-jsonschema - Read the Docs
validate () will first verify that the provided schema is itself valid, since not doing so can lead to less obvious error messages...
Read more >
Fields - ODMantic
Since by default MongoDB will create a field _id containing an ObjectId primary ... from typing import Union from odmantic import Model class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found