Skip to content

Schema path is not correctly escaped #727

@jirutka

Description

@jirutka

Describe the bug

The schema path in the path argument of the mutation function is not escaped.

To Reproduce

traverse({
  type: 'object',
  properties: {
    '.foo': {
      type: 'string'
    },
    '.': {
      type: 'object',
      properties: {
        'x': {
          type: 'number'
        }
      }
    },
    '[foo]': {
      type: 'string',
    },
  },
}, (schema, _isCycle, path) => {
  console.log(path)
  return schema
})
"$.properties..foo"
"$.properties...properties.x"
"$.properties.."
"$.properties.[foo]"
"$"

Expected behavior

"$.properties['.foo']"
"$.properties['.'].properties.x"
"$.properties['.']"
"$.properties['[foo]']"
"$"

…or use JSON Pointer instead of JSON Path:

"/properties/.foo"
"/properties/./properties/x"
"/properties/."
"/properties/[foo]"

…or just return an array of the path segments:

["properties", ".foo"]
["properties", ".", "properties", "x"]
["properties", "."]
["properties", "[foo]"]

JSON Path is not a good fit for this because it`s a query language, not an identifier, so it’s quite complex.

JSON Pointer is simpler and easier to work with. It has only two special characters: / which is escaped as ~1, and ~ which is escaped as ~0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions