JSON架构$ref不适用于相对路径
我有3个模式: 子架构:JSON架构$ref不适用于相对路径,json,jsonschema,json-schema-validator,Json,Jsonschema,Json Schema Validator,我有3个模式: 子架构: { "title": "child_schema", "type": "object", "properties": { "wyx":{ "type": "number" } }, "additionalProperties": false, "required": ["wyx"] } 父架构: { "title": "parent", "type
{
"title": "child_schema",
"type": "object",
"properties": {
"wyx":{
"type": "number"
}
},
"additionalProperties": false,
"required": ["wyx"]
}
父架构:
{
"title": "parent",
"type": "object",
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
},
"child": {
"$ref": "file:child.json"
}
}
}
架构:
{
"type": "object",
"title": "grandpa",
"properties": {
"reason": {
"$ref": "file:parent.json"
}
},
"additionalProperties": false
}
如您所见,gradpa有一个指向父对象的引用,父对象有一个指向子对象的引用。
这3个文件都在同一个文件夹中。
当我使用python验证器来验证模式时,我会不断得到一个名为RefResolutionError的错误
然而,如果我没有爷爷,我只使用父模式和子模式,一切都正常!!所以问题是我不能让一个ref指向一个ref(2级)。但我可以让一个ref指向一个模式(仅1级)
我想知道为什么你的推荐信不正确。如果引用的方案位于同一文件夹中,请使用简单的相对路径引用,如:
"child": {"$ref": "child.json"},
"reason": {"$ref": "parent.json"}
如果要使用进行验证,请不要忘记设置引用解析程序,以便解析引用架构的路径:
import os
from jsonschema import validate, RefResolver
instance = {}
schema = grandpa
# this is a directory name (root) where the 'grandpa' is located
schema_path = 'file:///{0}/'.format(
os.path.dirname(get_file_path(grandpa)).replace("\\", "/"))
resolver = RefResolver(schema_path, schema)
validate(instance, schema, resolver=resolver)
我在使用jaySchema模块时遇到了类似的问题,以下是我的解决方案: