Skip to content

feat: loosen z.strictObject() inside intersection#5726

Open
vatsal9117 wants to merge 1 commit intocolinhacks:mainfrom
vatsal9117:my-intersection-fix
Open

feat: loosen z.strictObject() inside intersection#5726
vatsal9117 wants to merge 1 commit intocolinhacks:mainfrom
vatsal9117:my-intersection-fix

Conversation

@vatsal9117
Copy link
Copy Markdown

What does this PR do?

This PR resolves an issue where z.strictObject() is functionally incompatible with z.intersection(). Previously, intersecting a strict object with another object would cause the strict object to throw an unrecognized_keys error for any keys defined by the other half of the intersection.

This update loosens the strictness exclusively within the context of intersections.

How it works:

In src/v4/core/schemas.ts, the handleIntersectionResults function now tracks unrecognized keys from both the left and right schemas using a tracking Map.

  • Any key recognized by at least one side of the intersection will now pass validation.
  • ZodError is only thrown for keys that are completely unrecognized by both sides.
  • Extra keys are stripped according to the standard object's default behavior if mixed.

Testing

Added test coverage in src/v4/classic/tests/intersection.test.ts to verify:

  • strict + strip behavior (allows shared keys, strips unknown)
  • strict + strict behavior (allows shared keys, throws on completely unknown keys)

@dosubot
Copy link
Copy Markdown

dosubot bot commented Feb 20, 2026

Related Documentation

Checked 0 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@vatsal9117
Copy link
Copy Markdown
Author

feat: loosen z.strictObject() inside intersection #5726

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

Successfully merging this pull request may close these issues.

1 participant