fix: discriminatedUnion encode() with codec discriminator#5769
Merged
colinhacks merged 1 commit intoApr 28, 2026
Merged
Conversation
When the discriminator field is a ZodCodec, the forward discriminator values (used during decode) differ from the backward values (used during encode). The fast discriminator map only contains forward values, so encode() fails with "No matching discriminator". Fall back to union matching when the fast-path fails in backward direction, allowing codec-based discriminators to work correctly. Fixes colinhacks#5593
Owner
|
@pullfrog review |
Contributor
There was a problem hiding this comment.
Reviewed — no issues found.
Clean, minimal fix. The fast discriminator path is still tried first, so non-codec discriminated unions pay no cost during encode(). The fallback to union matching only triggers when the fast lookup genuinely misses (the codec case). Test covers both directions.
Task list (6/6 completed)
- Checkout PR and read the diff
- Read and analyze the changed files in detail
- Review linked issue #5593 for context
- Verify correctness of the fix approach
- Review tests for completeness
- Submit review
Claude Opus | 𝕏
Owner
|
Clean fix. The fast-path map is still tried first for Note: this comment was produced by an AI coding assistant. |
Owner
|
Landed in Zod 4.4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Fixes #5593
When the discriminator field is a
ZodCodec,encode()fails with "No matching discriminator" because the fast discriminator map only contains forward (decode) values while the encode input has backward (output) values.Before
After
Approach
The discriminator map is precomputed from forward
propValues— for codecs, these are the input schema values (1,2). Duringencode(), the discriminator comes from the output schema ("one","two"), so the fast-path lookup misses.Rather than building a second backward discriminator map (which adds complexity and may not generalize to all codec patterns), this fix falls back to the union matching path when the fast discriminator lookup fails in backward direction. This is the same path that
unionFallback: truealready uses.Files changed
packages/zod/src/v4/core/schemas.ts— 3-line change in$ZodDiscriminatedUnion.parsepackages/zod/src/v4/classic/tests/discriminated-unions.test.ts— new test caseTest plan