Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON架构$ref不适用于相对路径_Json_Jsonschema_Json Schema Validator - Fatal编程技术网

JSON架构$ref不适用于相对路径

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

我有3个模式:

子架构:

{
    "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模块时遇到了类似的问题,以下是我的解决方案: