python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.6k stars 578 forks source link

Relative pointers fail to evaluate #1265

Closed stefanklug closed 4 months ago

stefanklug commented 4 months ago

Hi all,

first thanks for this great module. I'm pretty new to json schema, so bear with me if I got something wrong. I try to use a relative pointer. According to https://opis.io/json-schema/2.x/pointers.html#relative-pointers this should be a valid schema definition, but I get a jsonschema.exceptions._WrappedReferencingError.

My failing testcase is:

import json
import jsonschema

raw_schema = r'''
{
  "type": "object",
  "properties": {
    "first_email": {
      "type": "string",
      "format": "email"
    },
    "second_email": {
      "$ref": "1/first_email"
    }
  }
}
'''

raw_doc = '''
{"first_email": "john@example.com", "second_email": "opis@example.com"}
'''

schema = json.loads(raw_schema)
doc = json.loads(raw_doc)

# this should succeed, but fails with jsonschema.exceptions._WrappedReferencingError: Unresolvable: 1/first_email
jsonschema.validate(doc, schema)

This was tested with version 4.22.0

Is this behavior expected?

Kind regards, Stefan

Julian commented 4 months ago

Hey! Thanks for the kind words.

Yes no current version of JSON Schema supports relative pointers anywhere other than in the format keyword.

Julian commented 4 months ago

More specifically the PHP page you linked seems simply incorrect under the spec.

stefanklug commented 4 months ago

Oh bad luck, so this is actually no bug and I have to put everything under $defs. Correct?

Julian commented 4 months ago

It doesn't have to go in $defs, you could also use $anchors but yeah there's no such thing as relative referencing right now.

stefanklug commented 4 months ago

Thanks for your quick help :smile: