Skip to content

_Error.json_path produces invalid or questionable JSON paths #1389

@kurtmckee

Description

@kurtmckee

The json_path property on the _Error class does not escape or otherwise account for property names that are either blank or have non-alphanumeric characters.

For example, a property named . currently renders to JSON path as $... It should instead be $['.'].

Here is a script that demonstrates the json_path rendering of various valid property names, which result in invalid or questionable JSON paths:

import jsonschema


# Keys that currently produce invalid or questionable JSON paths.
keys = ["", "[", ".", "\\", "'", " ", "\"", "a[0]"]

schema = {
    # Require that each key be a string.
    "properties": {key: {"type": "string"} for key in keys},
}

instance = {
    # Set each key to a non-string value.
    key: None for key in keys
}

validator = jsonschema.Draft7Validator(schema)
for error in validator.iter_errors(instance):
    print(f"property={error.path[-1]!r}\t\tjson_path={error.json_path!r}")

And the output:

$ python demo.py 
property=''             json_path='$.'
property='['            json_path='$.['
property='.'            json_path='$..'
property='\\'           json_path='$.\\'
property="'"            json_path="$.'"
property=' '            json_path='$. '
property='"'            json_path='$."'
property='a[0]'         json_path='$.a[0]'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions