Fix optional()/nullable() crashing on absent dynamic() values - #1302
Open
yhuikzdtguioaert wants to merge 1 commit into
Open
yhuikzdtguioaert wants to merge 1 commit into
yhuikzdtguioaert wants to merge 1 commit into
Conversation
`optional()` and `nullable()` only guard their `validator` and `refiner`
against `undefined`/`null`; they inherit the wrapped struct's `entries`
and `coercer` through the object spread. So when the value is absent the
inner struct is still traversed and coerced.
For a `dynamic()` inner this throws, because its `entries`/`coercer`
invoke the selector callback with the absent value. For example:
assert({}, partial(object({ user: dynamic((v) => v.kind === 'user' ? User : Bot) })))
crashes with `TypeError: Cannot read properties of undefined` instead of
accepting the missing optional property.
Guard `entries` and `coercer` so an `undefined` (optional) or `null`
(nullable) value short-circuits without touching the inner struct.
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.
Fixes #1294.
Problem
optional()andnullable()only guard theirvalidatorandrefineragainstundefined/null. They copy the wrapped struct'sentriesandcoercerthrough the object spread, so when the value is absent the inner struct is still traversed and coerced.That is harmless for most structs, whose
entriesbail out on a non-matching value, butdynamic()runs its selector callback unconditionally. So an absent value reaches user code that assumes it is present:The same happens for
optional(dynamic(...))withundefinedandnullable(dynamic(...))withnull, on thecreate()path (via the inherited coercer) as well asassert()/is()/validate().Fix
Guard
entriesandcoercerinoptional()andnullable()so anundefined(optional) ornull(nullable) value short-circuits without descending into the inner struct, matching howvalidatorandrefineralready treat the absent value as valid.Tests
Added validation fixtures for the reported
partial(object({ ...dynamic }))case plusoptional(dynamic())andnullable(dynamic())directly, covering both the assert and create paths. The full suite passes (npm run test:vitest, andtscover the test project).