Repository navigation
feat: support oneOf - #267
Open
belfordzjeeves wants to merge 1 commit into
Open
belfordzjeeves wants to merge 1 commit into
belfordzjeeves wants to merge 1 commit into
Conversation
validateItem now counts branches matching the data; exactly one match validates, otherwise a OneOfValidationError is reported. The validator's traverse walk skips subschemas beneath oneOf keywords (they are handled recursively at the oneOf schema) so branches are no longer checked against the parent schema's data.
This was referenced Oct 19, 2026
This branch has not been deployed
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 #201
Root cause (two parts)
validateIteminsrc/index.tsonly dispatched onschema.type; a schema carryingoneOf(and notype) silently validated as true at its own level.traverse(schema, ...)walk invalidator()visited each branch at paths like$.oneOf[0], andschemaPathToRegularPathdrops theoneOf[0]token, so every branch subschema was validated against the parent's data — producing false errors (e.g. the{type: "number"}branch checked against"potato").Fix
src/base-validators/oneOf.ts(new):OneOfValidationErrorfollowing the existing error-class pattern; added to theValidationErrorunion insrc/index.ts.validateItemnow handlesoneOf: it counts branches wherevalidateItem(branch, data) === true. Exactly one match returns true; otherwise aOneOfValidationErroris collected with reasonexpected data to match exactly one schema in oneOf, but N matched. Recursion throughvalidateItemmakes nested oneOf (in a branch, in an object property) work.validator()traverse callback skips subschemas sitting under aoneOfkeyword (matched by/(^|\.)oneOf\[\d+\]/), sincevalidateItemhandles them recursively at the oneOf schema. A data property literally namedoneOfyields paths like$.properties.oneOf(no bracket index) and is deliberately not skipped — covered by a precision test.allOf/anyOf/notuntouched.Verification
ValidationErrors; after fix both returntrue.oneOf.tsis 100% covered.npx tsc --noEmitclean.src/index.test.ts: the issue's two cases verbatim, both-branches-match, neither-matches, oneOf in an object property (valid + invalid), oneOf nested in a oneOf branch, and the property-literally-named-oneOfprecision test.