23

I have 3 schemas:

child schema:

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

parent schema:

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

grandpa schema:

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

As you can see, gradpa has a ref to parent and parent has a ref to child. All these 3 files are inside the same folder. When I use python validator to validate grandpa schema, I will keep on getting an error called RefResolutionError.

HOWEVER, if I do not have grandpa and I just use parent schema and child schema, everything worked!! So the issue is I cannot have a ref pointing to a ref(2 levels). But I can have a ref pointing to a schema(just 1 level.)

I wonder why

ahri
  • 387
  • 1
  • 5
  • 11
  • I've a problem like this using the module jaySchema and here is my solution: http://stackoverflow.com/questions/26920482/json-schema-referencing-a-local-child-schema/27024626#27024626 – Djiby Thiaw Nov 20 '14 at 09:50

2 Answers2

26

Your references are incorrect. If the referenced schemes are in the same folder, use simple relative path references like:

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

If you're using jsonschema for validation, don't forget to set a reference resolver in order to resolve the paths to referenced schemas:

    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)
Magnus
  • 3
  • 1
  • 2
Alex-Bogdanov
  • 2,172
  • 1
  • 19
  • 20
0

This is what I used to dynamically generate a jsonschema schema_store from all schemas in a given directory

child.schema.json

{
    "$id": "child.schema.json",
    "title": "child_schema",
    "type": "object",
    "properties": {
        "wyx":{
            "type": "number"
        }
     },
    "additionalProperties": false,
    "required": ["wyx"]
}

parent.schema.json

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

instance.json

{
    "x": 1,
    "y": 2,
    "child": {
        "wyx": 3
    }
}

validator.py

import json

from pathlib import Path

from jsonschema import Draft7Validator, RefResolver
from jsonschema.exceptions import RefResolutionError

schemas = (json.load(open(source)) for source in Path("schema/dir").iterdir())
schema_store = {schema["$id"]: schema for schema in schemas}

schema = json.load(open("schema/dir/extend.schema.json"))
instance = json.load(open("instance/dir/instance.json"))
resolver = RefResolver.from_schema(schema, store=schema_store)
validator = Draft7Validator(schema, resolver=resolver)

try:
    errors = sorted(validator.iter_errors(instance), key=lambda e: e.path)
except RefResolutionError as e:
    print(e)
reubano
  • 5,087
  • 1
  • 42
  • 41