JSON模式$ ref不适用于相对路径

ahr*_*hri 10 json jsonschema json-schema-validator

我有3个架构:

子模式:

{
    "title": "child_schema",
    "type": "object",
    "properties": {
        "wyx":{
            "type": "number"
        }
     },
    "additionalProperties": false,
    "required": ["wyx"]
}
Run Code Online (Sandbox Code Playgroud)

父模式:

{
    "title": "parent",
    "type": "object",
    "properties": {
        "x": {
            "type": "number"
        },
        "y": {
            "type": "number"
        },
        "child": {
            "$ref": "file:child.json"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

爷爷架构:

{
    "type": "object",
    "title": "grandpa",
    "properties": {
        "reason": {
            "$ref": "file:parent.json"
        }
    },
    "additionalProperties": false
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,gradpa有一个父母的参考,父母有一个参考孩子.所有这3个文件都在同一个文件夹中.当我使用python验证器验证爷爷架构时,我将继续收到一个名为RefResolutionError的错误.

但是,如果我没有爷爷,我只使用父模式和子模式,一切正常!所以问题是我不能指向ref(2级)的ref.但是我可以有一个指向模式的引用(只有1级).

我想知道为什么

Ale*_*nov 14

您的推荐不正确.如果引用的方案位于同一文件夹中,请使用简单的相对路径引用,如:

"child": {"$ref": "child.json"},
"reason": {"$ref": "parent.json"}
Run Code Online (Sandbox Code Playgroud)

如果您使用jsonschema进行验证,请不要忘记设置引用解析程序以解析引用模式的路径:

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)
Run Code Online (Sandbox Code Playgroud)