Skip to content

fix: support dashes/spaces in property names - #268

Open
belfordzjeeves wants to merge 1 commit into
masterfrom
fix/198-property-names-dashes-spaces
Open

belfordzjeeves wants to merge 1 commit into
masterfrom
fix/198-property-names-dashes-spaces

Conversation

@belfordzjeeves

Copy link
Copy Markdown
Contributor

Fixes #198

Root cause

schemaPathToRegularPath in src/index.ts converted traverse paths to data
jsonpaths by splitting on . and only understanding bare properties/items
tokens. Since traverse >= 1.11, non-identifier property keys are emitted
bracket-quoted (e.g. $.properties["Some-Prop"]). That token matched neither
branch and was silently dropped, so the property subschema was validated
against the whole data object instead of data["Some-Prop"]: valid data
failed 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 .); quoted
    names may contain dashes, spaces, dots, or escaped quotes (unescaped via
    JSON.parse).
  • Property names are emitted in jsonpath bracket notation:
    $.properties["Some-Prop"].items[0] -> $["Some-Prop"][0],
    $.properties["a"]["b c"] -> $["a"]["b c"].
  • Preserved behavior: bare properties ($.properties.foo -> $["foo"]),
    single-schema items ([*]), tuple items[n] ([n]), and non-properties
    keywords (allOf[n], patternProperties, ...) still map to the enclosing
    data as before.
  • The segment after properties is always treated as a property name, even
    when the name itself is properties or items.
  • Names containing a double quote are emitted single-quoted ($['a"b'])
    because the jsonpath lib does not process escapes inside "...".

Verification

  • Repro from Doesn't support schemas with dashes/spaces in property names #198 before: validator(schema, { "Some-Prop": "ok" }) returned
    ValidationErrors; after: returns true. Wrong-type data still returns
    ValidationErrors with exactly one StringValidationError for the property
    itself.
  • Full suite: 48/48 tests pass (38 existing + 10 new regression tests: dashed,
    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).
  • Coverage thresholds met: statements 97.14% (>= 94.28), branches 93.25%
    (>= 87.77), functions 100%, lines 96.98% (>= 93.99). New code is fully
    covered; the only uncovered lines in src/index.ts are pre-existing
    validateItem branches.
  • npx tsc --noEmit passes.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Doesn't support schemas with dashes/spaces in property names

1 participant