3

I'm starting using python to validate some json information, i'm using a json schema with reference but i'm having trouble to reference those files. This is the code :

from os.path import join, dirname
from jsonschema import validate
import jsonref
def assert_valid_schema(data, schema_file):
    """ Checks whether the given data matches the schema """

    schema = _load_json_schema(schema_file)
    return validate(data, schema)

def _load_json_schema(filename):
    """ Loads the given schema file """

    relative_path = join('schemas', filename).replace("\\", "/")
    absolute_path = join(dirname(__file__), relative_path).replace("\\", "/")

    base_path = dirname(absolute_path)
    base_uri = 'file://{}/'.format(base_path)

    with open(absolute_path) as schema_file:
        return jsonref.loads(schema_file.read(), base_uri=base_uri, jsonschema=True, )

assert_valid_schema(data, 'grandpa.json')

The json data is :

data = {"id":1,"work":{"id":10,"name":"Miroirs","composer":{"id":100,"name":"Maurice Ravel","functions":["Composer"]}},"recording_artists":[{"id":101,"name":"Alexandre Tharaud","functions":["Piano"]},{"id":102,"name":"Jean-Martial Golaz","functions":["Engineer","Producer"]}]}

And i'm saving the schema and reference file, into a schemas folder :

recording.json :

{"$schema":"http://json-schema.org/draft-04/schema#","title":"Schema for a recording","type":"object","properties":{"id":{"type":"number"},"work":{"type":"object","properties":{"id":{"type":"number"},"name":{"type":"string"},"composer":{"$ref":"artist.json"}}},"recording_artists":{"type":"array","items":{"$ref":"artist.json"}}},"required":["id","work","recording_artists"]}

artist.json :

{"$schema":"http://json-schema.org/draft-04/schema#","title":"Schema for an artist","type":"object","properties":{"id":{"type":"number"},"name":{"type":"string"},"functions":{"type":"array","items":{"type":"string"}}},"required":["id","name","functions"]}

And this is my error :

Connected to pydev debugger (build 181.5281.24)
Traceback (most recent call last):
  File "C:\Python\lib\site-packages\proxytypes.py", line 207, in __subject__
    return self.cache
  File "C:\Python\lib\site-packages\proxytypes.py", line 131, in __getattribute__
    return _oga(self, attr)
AttributeError: 'JsonRef' object has no attribute 'cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python\lib\site-packages\jsonref.py", line 163, in callback
    base_doc = self.loader(uri)
<MORE>

python version : 3.6.5

windows 7

Ide : intellijIdea

Can somebody help me? Thank you

3
  • I can't provide an answer as I'm not familiar with this implementation, but a JSON Schema is not aware of where its file is located, so an implementation will not by default know how to resolve those references! You may just need to set the $id of the schema, or you may need to add the schemas to the libraries instance using an API. There's not much documentation about how to do this with that implementation: python-jsonschema.readthedocs.io/en/latest/references Commented Jul 11, 2018 at 8:49
  • I'll nudge some people on the slack to come and take a look =] Commented Jul 11, 2018 at 8:50
  • Version 0.2 of the jsonref library might be broken/incompatible in some strange way. Try pip install jsonref==0.1... Commented Dec 6, 2018 at 20:06

1 Answer 1

1

I am not sure why, but on Windows, the file:// needs an extra /. So the following change should do the trick

base_uri = 'file:///{}/'.format(base_path)

Arrived at this answer from a solution posted for a related issue in json schema

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.