Skip to content

Fix (Value): Union encode selects member matching the unencoded value (#1706) - #1708

Open
cpruijsen wants to merge 1 commit into
sinclairzx81:mainfrom
cpruijsen:fix/issue-1706-40268999
Open

cpruijsen wants to merge 1 commit into
sinclairzx81:mainfrom
cpruijsen:fix/issue-1706-40268999

Conversation

@cpruijsen

Copy link
Copy Markdown

Value.Encode and EncodeUnsafe now try union members that accept the unencoded value first (src/value/codec/from_union.ts).
For Union([Codec, Null]), a null input returns null without a call to the codec's encoder.
Previously, a strict codec could throw, while a JSON codec could return the string "null".

Each member attempt receives a clone and must produce output that passes that member's check.
The fallback handles decoded Date and object values that do not match a member before encoding.
The fallback skips members already attempted in the first pass.

Union([Codec(String), Number]) now encodes numeric 5 as 5 when the codec decodes to numbers.
Previously, that codec could return "5" first.
The extra input checks can increase validation cost.

The strict Date and JSON null regressions in test/typebox/runtime/value/codec/union.ts fail with the merge-base source and pass with this change.

Fixes #1706.

@sinclairzx81
sinclairzx81 force-pushed the main branch 2 times, most recently from ea3655a to 54ab51d Compare October 17, 2026 10:45

This branch has not been deployed

No deployments
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.

Union encode picks a codec member for null in Union([Codec, Null])

1 participant