我们如何使用 jsonschema.RefResolver 验证模式中的多个引用?
我有一个验证脚本,如果我在文件中有一个引用,它就可以很好地工作。我现在在一个架构中有两个或三个引用,它们位于不同的目录中。
base_dir = '/schema/models/'
with open (os.path.join(base_dir, 'Defined.json')) as file_object:
schema = json.load(file_object)
resolver = jsonschema.RefResolver('file://' + base_dir + '/' + 'Fields/Ranges.json', schema)
jsonschema.Draft4Validator(schema, resolver=resolver).validate(data)
Run Code Online (Sandbox Code Playgroud)
我的 json 架构:
{
"properties": {
"description": {
"type": "object",
"after": {"type": ["string", "null"]},
"before": {"type": "string"}
},
"width": {"type": "number"} ,
"range_specifier": {"type": "string"},
"start": {"type": "number", "enum" : [0, 1] } ,
"ranges": {
"$ref": "Fields/Ranges.json"
},
"values": {
"$ref": "Fields/Values.json"
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是我应该有两个解析器,一个用于范围,一个用于值,并在 Draft4Validator 中分别调用解析器?或者有没有更好的方法来做到这一点?