Repository navigation
fix: support dashes/spaces in property names - #268
Open
belfordzjeeves wants to merge 1 commit into
Open
belfordzjeeves wants to merge 1 commit into
belfordzjeeves wants to merge 1 commit into
Conversation
Fixes #198. schemaPathToRegularPath split traverse paths on '.', which broke once traverse >= 1.11 started emitting bracket-quoted segments ($['Some-Prop']) for non-identifier keys: the quoted token matched neither the 'properties' nor the 'items' branch and was dropped, so the property subschema was validated against the whole data object. The path conversion now tokenizes traverse paths properly (quoted segments are never split on '.'; escaped quotes unescaped) and emits jsonpath bracket notation for property names, e.g. $.properties['Some-Prop'].items[0] -> $['Some-Prop'][0]. Preserves existing behavior for bare properties, single-schema items ([*]), tuple items[n], and non-properties keywords (allOf[n], ...).
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 #198
Root cause
schemaPathToRegularPathinsrc/index.tsconverted traverse paths to datajsonpaths by splitting on
.and only understanding bareproperties/itemstokens. Since traverse >= 1.11, non-identifier property keys are emitted
bracket-quoted (e.g.
$.properties["Some-Prop"]). That token matched neitherbranch and was silently dropped, so the property subschema was validated
against the whole data object instead of
data["Some-Prop"]: valid datafailed validation, and invalid data failed for the wrong reason.
Fix
Rewrote the path conversion to properly tokenize traverse paths:
["quoted"]segments are parsed as one token (never split on.); quotednames may contain dashes, spaces, dots, or escaped quotes (unescaped via
JSON.parse).
$.properties["Some-Prop"].items[0]->$["Some-Prop"][0],$.properties["a"]["b c"]->$["a"]["b c"].properties($.properties.foo->$["foo"]),single-schema
items([*]), tupleitems[n]([n]), and non-propertieskeywords (
allOf[n],patternProperties, ...) still map to the enclosingdata as before.
propertiesis always treated as a property name, evenwhen the name itself is
propertiesoritems.$['a"b'])because the jsonpath lib does not process escapes inside
"...".Verification
validator(schema, { "Some-Prop": "ok" })returnedValidationErrors; after: returnstrue. Wrong-type data still returnsValidationErrorswith exactly oneStringValidationErrorfor the propertyitself.
spaced, dotted, and double-quoted property names; nested dashed properties;
dashed property in array items and tuple items; dashed property in allOf;
property literally named
items).(>= 87.77), functions 100%, lines 96.98% (>= 93.99). New code is fully
covered; the only uncovered lines in
src/index.tsare pre-existingvalidateItembranches.npx tsc --noEmitpasses.