多阶段$ ref不适用于jsonschema版本3.0.1

时间:2019-05-17 08:16:53

标签: jsonschema python-jsonschema

似乎jsonschema 3.0.1版不接受使用$ refs的多阶段模式(虽然它与jsonschema 2.6.0版一起使用)。

仅因为我的代码将在具有不同环境的不同计算机上运行,​​我必须使其在多个模块版本下工作。

我在https://www.jsonschemavalidator.net/上验证了我的json(感谢在另一个StackOverflow问题中找到此链接)。

我尝试过:

  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

注意: * .schema.json都适用于https://www.jsonschemavalidator.net/

文件myjson.json:

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

文件noRefs.schema.json:

{
  "type": "object",

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

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

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

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"
  }

}

收到错误消息:

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

1 个答案:

答案 0 :(得分:1)

编辑:我之前的答案不正确。

TL; DR:您有两个选择:

  1. 从定义中删除$id属性
  2. #/属性中使用$id(例如:{"$id": "#/justAString"}

详细信息:

问题在于ID,直到草稿04为止,$ref$id都以票面价值对待,没什么特别的,但是从草稿06开始,这些是uri-reference ,在这种情况下,解析到{"$id": "schemaTopProperty"}时,不再{"$ref": "justAString"}在根结构中寻找片段justAString,而是在/justAString下寻找schemaTopProperty主机,这是一个远程参考。

因此,我的解决方案可以删除导致定义为URL(实际上是主机)的$id,或者将$id定义为当前架构中的片段。