zod icon indicating copy to clipboard operation
zod copied to clipboard

Wrong error when using a union with strict objects

Open trygveaa opened this issue 3 years ago • 9 comments

I have two object types with one key each (with different names), and I want to parse an object and verify that it has one and only one of those keys. To do that, I created a union schema with two strict objects:

z.union([
  z.object({ a: z.number() }).strict(),
  z.object({ b: z.number() }).strict()
])

If I parse { a: 1 } or { b: 1 } it validates successfully as expected, and if I parse { c: 1 } it gives an invalid_union error with the issue from both union elements in unionErrors as expected.

However if I parse { a: 1, b: 1 } I don't get an invalid_union error. I instead get an unrecognized_keys error, i.e. the same error you would get if you parsed this directly with z.object({ a: z.number() }).strict(). I also see that in other to change this error message, I have to add an errorMap to the object schema, rather than the union schema. Since none of the object schemas match the input, I would expect to get an invalid_union here as well.

This makes it hard to get a useful error message for this use case.

It seems like what's happening is that when checking if any of the union schemas is valid, it doesn't consider them to be strict, then selects the first since it matches when it's not strict, and then applies strict afterwards?

trygveaa avatar Jan 19 '22 10:01 trygveaa

This is a nice find, thanks for the detailed reporting and troubleshooting. I think you're right that we should treat this as a union error. I don't have any great ideas right off the top of my head, but PRs definitely welcome for tidying this up!

scotttrinh avatar Jan 19 '22 18:01 scotttrinh

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[bot] avatar Apr 22 '22 21:04 stale[bot]

I opened #872 to fix this a couple of months ago.

trygveaa avatar Apr 23 '22 02:04 trygveaa

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[bot] avatar Jun 22 '22 03:06 stale[bot]

Still relevant, the PR hasn't been reviewed.

trygveaa avatar Jun 22 '22 15:06 trygveaa

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[bot] avatar Aug 30 '22 23:08 stale[bot]

Still relevant, the PR hasn't been reviewed.

trygveaa avatar Aug 31 '22 09:08 trygveaa

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[bot] avatar Oct 30 '22 19:10 stale[bot]

...

trygveaa avatar Oct 30 '22 19:10 trygveaa

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[bot] avatar Jan 28 '23 22:01 stale[bot]