fix(schema): prevent crash validating unions of object arrays#1638
Open
xianjianlf2 wants to merge 1 commit into
Open
fix(schema): prevent crash validating unions of object arrays#1638xianjianlf2 wants to merge 1 commit into
xianjianlf2 wants to merge 1 commit into
Conversation
When a union of array types with object element bases (e.g.
`{ objB: string }[] | { objC: string }[]`) was validated against an
array containing a non-object element, a `TypeError: Cannot use 'in'
operator` was thrown.
Inside a union branch, traversal runs in fail-fast mode and
`ctx.currentErrorCount` saturates at 1, so an intersection's basis
(object/proto) failure guard (`currentErrorCount > errorCount`) could
not detect a newly added error once an earlier element in the same
branch had already errored. The sequence node then continued iterating
and applied the element's structure check (`key in data`) to a
primitive, throwing.
SequenceNode now mirrors StructureNode by bailing out of remaining
elements in fail-fast mode, both in `traverseApply` and in its compiled
output, so a failed element basis short-circuits before the unguarded
`in` check runs. Top-level (non-fail-fast) validation is unchanged and
still collects every element error.
Closes arktypeio#1458
Contributor
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — this PR prevents a hard TypeError crash when validating a union of object arrays against an array containing a primitive element, by making SequenceNode bail out of further element traversal in fail-fast mode (e.g. inside a union branch), matching the behavior already implemented in StructureNode.
SequenceNode.traverseApplynow checksctx.failFast && ctx.currentErrorCount > errorCountafter each element and returns early, stopping traversal of later elements once one element has failed inside a fail-fast context.SequenceNode.compilenow initializes anerrorCountsnapshot and emitsjs.returnIfFailFast()after each prefix, defaultable/optional, variadic, and postfix element in the generatedApplytraversal, producing the compiled equivalent of the runtime short-circuit.- Regression test added to
ark/type/__tests__/union.test.tscovering both element-orderings from issue #1458.
Kimi K2 (free via Pullfrog for OSS) | 𝕏
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.

Problem
When a union of array types with object element bases (e.g.
{ objB: string }[] | { objC: string }[]) was validated against an array containing a non-object element, aTypeError: Cannot use 'in' operatorwas thrown.Inside a union branch, traversal runs in fail-fast mode and
ctx.currentErrorCountsaturates at 1, so an intersection's basis (object/proto) failure guard (currentErrorCount > errorCount) could not detect a newly added error once an earlier element in the same branch had already errored. The sequence node then continued iterating and applied the element's structure check (key in data) to a primitive, throwing.Fix
SequenceNodenow mirrorsStructureNodeby bailing out of remaining elements in fail-fast mode, both intraverseApplyand in its compiled output, so a failed element basis short-circuits before the unguardedincheck runs. Top-level (non-fail-fast) validation is unchanged and still collects every element error.Testing
Added a regression test in
ark/type/__tests__/union.test.tscovering a union of object arrays validated against an array containing a non-object element, confirming it no longer throws and reports a proper validation error.Closes #1458