Repository navigation
fix(schema): prevent crash validating unions of object arrays - #1638
Merged
ssalbdivad merged 1 commit intoSep 18, 2026
Merged
ssalbdivad merged 1 commit into
ssalbdivad merged 1 commit into
Conversation
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) | 𝕏
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
ssalbdivad
force-pushed
the
fix/array-union-in-operator-crash-1458
branch
from
September 18, 2026 18:36
2f6a097 to
788981b
Compare
Member
|
@xianjianlf2 thanks so much great find and the fix is perfect! |
4 tasks done
ssalbdivad
added a commit
that referenced
this pull request
Sep 24, 2026
Bumps all publishable packages and documents the changes merged since 2.2.4: - preserve escaped backslashes in regex literals (#1634, @spokodev) - fix recursive discriminated unions referenced from a Record (#1641, @xianjianlf2) - prevent a crash validating unions of object arrays (#1638, @xianjianlf2) - merge index-derived props with a declared key of the same name (#1659) - preserve parameter labels when a type.fn implementation annotates an optional param (#1615, @aswinsvijay) #1634 changes behavior for regex literals that contain `\\`: `"/\\\\d/"` previously compiled to the digit class `\d` and now matches a literal backslash followed by "d", matching the equivalent RegExp instance. The changelog calls this out with a migration note. Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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