1

It seems that jsonschema version 3.0.1 does not accept multi-stage schema using $refs (while it works with jsonschema version 2.6.0).

I have to make it work under several module versions simply because my code will be running on different computers with different environments.

I verified my jsons on https://www.jsonschemavalidator.net/ (thanks for this link found in another StackOverflow question).

I Tried :

  jsonschema -i myjson.json noRefs.schema.json      --> 2.6.0 = OK, 3.0.1 OK
  jsonschema -i myjson.json usingRefs.schema.json   --> 2.6.0 = OK, 3.0.1 KO

Note : Both *.schema.json worked on https://www.jsonschemavalidator.net/

File myjson.json :

{
  "TopProperty" : {
    "LowerProperty" : {"toto" : "plop"}
  }
}

File noRefs.schema.json :

{
  "type": "object",

  "properties": {
    "TopProperty": {"$ref": "#/schemaTopProperty"}
  },

  "schemaTopProperty": {
    "$id": "schemaTopProperty",
    "type": "object",

    "properties": {
      "LowerProperty": {
        "type": "object",
        "properties": {
          "toto": {"type": "string"}
        }
      }
    }
  }
}

File usingRefs.schema.json :

{
  "type": "object",

  "properties": {
    "TopProperty": {"$ref": "#/schemaTopProperty"}
  },

  "schemaTopProperty": {
    "$id": "schemaTopProperty",
    "type": "object",

    "properties": {
      "LowerProperty": {
        "type": "object",
        "properties": {
          "toto": {"$ref": "#/justAString"}
        }
      }
    }
  },

  "justAString": {
    "$id": "justAString",
    "type": "string"
  }

}

Error message received :

Traceback (most recent call last):                                                                                                         
  File "/usr/bin/jsonschema", line 11, in <module>                                                                                         
    sys.exit(main())                                                                                                                       
  File "/usr/lib/python2.7/site-packages/jsonschema/cli.py", line 67, in main                                                              
    sys.exit(run(arguments=parse_args(args=args)))                                                                                         
  File "/usr/lib/python2.7/site-packages/jsonschema/cli.py", line 78, in run                                                               
    for error in validator.iter_errors(instance):                                                                                          
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors                                               
    for error in errors:                                                                                                                   
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 274, in properties                                               
    schema_path=property,                                                                                                                  
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend                                                   
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 251, in ref
    for error in validator.descend(instance, resolved):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 274, in properties
    schema_path=property,
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 73, in items
    for error in validator.descend(item, items, path=index):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 274, in properties
    schema_path=property,
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 247, in ref
    scope, resolved = validator.resolver.resolve(ref)
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 734, in resolve
    return url, self._remote_cache(url)
  File "/usr/lib/python2.7/site-packages/functools32/functools32.py", line 400, in wrapper
    result = user_function(*args, **kwds)
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 744, in resolve_from_url
    raise exceptions.RefResolutionError(exc)
jsonschema.exceptions.RefResolutionError: unknown url type: schemaTopProperty
  • There's debate over what is a valid object to reference. Have you tried putting the properties you want to reference inside a `definitions` object? Assuming this is draft-7 here. – Relequestual May 17 '19 at 08:26
  • Following [link](https://json-schema.org/understanding-json-schema/structuring.html), I've tried to embed my `justAString`object into a `definitions` one but I get the same error message. – Gabriel Georges May 17 '19 at 08:49
  • I've simplified my files, hope it will be clearer. – Gabriel Georges May 20 '19 at 13:46
  • I tested your schema using https://jsonschema.dev and it seemed to work fine. Looks like this is an implementation issue and you should log an issue on the implementation issue tracker. I've notified the author of the implementation on our slack server, but I can't say when they will respond. – Relequestual May 20 '19 at 13:52
  • Thank you for testing. I have written a post on this [google forum](https://groups.google.com/forum/#!forum/jsonschema). I will look to Slack too. – Gabriel Georges May 20 '19 at 14:38
  • We monitor that also. Julian is best placed to answer your question. Good luck! =] – Relequestual May 20 '19 at 14:47

1 Answers1

2

Edit: my previous answer was incorrect.

TL;DR: You have two options:

  1. Remove the $id properties from the definitions
  2. Use #/ in the $id properties (Example: {"$id": "#/justAString"})

Details:

The issue is with the IDs, up until draft-04, $ref and $id were treated at face value, nothing special, but starting with draft-06 these are uri-references, in which case, when descending into {"$id": "schemaTopProperty"}, resolving {"$ref": "justAString"} is no more looking for a fragment justAString at the root structure, but for /justAString under schemaTopProperty host, which is a remote reference.

Hence my solutions to either remove the $ids which cause the definitions to be URLs (hosts in fact), or to define the $ids as what they are, fragments in the current schema.