JSON $ ref scheme not working for relative path

I have 3 schemes:

child schema:

{
    "title": "child_schema",
    "type": "object",
    "properties": {
        "wyx":{
            "type": "number"
        }
     },
    "additionalProperties": false,
    "required": ["wyx"]
}

parent scheme:

{
    "title": "parent",
    "type": "object",
    "properties": {
        "x": {
            "type": "number"
        },
        "y": {
            "type": "number"
        },
        "child": {
            "$ref": "file:child.json"
        }
    }
}

grandfather scheme:

{
    "type": "object",
    "title": "grandpa",
    "properties": {
        "reason": {
            "$ref": "file:parent.json"
        }
    },
    "additionalProperties": false
}

As you can see, gradpa has a ref link for the parent, and the parent has a ref link. All these 3 files are in the same folder. When I use the python validator to validate the grandpa schema, I will continue to get an error called RefResolutionError.

HOWEVER, if I don’t have a grandfather, and I just use the parent scheme and child scheme, everything works! So the problem is that I cannot reference ref (2 levels). But I can have a ref link pointing to a schema (only 1 level).

I wonder why

+4
source share
1

. , , :

"child": {"$ref": "child.json"},
"reason": {"$ref": "parent.json"}

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)
+7

All Articles