Skip to content

Instantly share code, notes, and snippets.

@mrtj
Last active March 13, 2022 14:07
Show Gist options
  • Save mrtj/d59812a981da17fbaa67b7de98ac3d4b to your computer and use it in GitHub Desktop.
Save mrtj/d59812a981da17fbaa67b7de98ac3d4b to your computer and use it in GitHub Desktop.
Use local references in jsonschema python package
import os
import json
from pathlib import Path
from urllib.parse import urljoin
import jsonschema
def add_local_schemas_to(resolver, schema_folder, base_uri, schema_ext='.schema.json'):
''' Add local schema instances to a resolver schema cache.
Arguments:
resolver (jsonschema.RefResolver): the reference resolver
schema_folder (str): the local folder of the schemas.
base_uri (str): the base URL that you actually use in your '$id' tags
in the schemas
schema_ext (str): filter files with this extension in the schema_folder
'''
for dir, _, files in os.walk(schema_folder):
for file in files:
if file.endswith(schema_ext):
schema_path = Path(dir) / Path(file)
rel_path = schema_path.relative_to(schema_folder)
with open(schema_path) as schema_file:
schema_doc = json.load(schema_file)
key = urljoin(base_uri, str(rel_path))
resolver.store[key] = schema_doc
# Example usage for add_local_schemas_to
import json
import jsonschema
instance_filename = 'data.json'
schema_folder = Path('schemas')
schema_filename = schema_folder / 'root.schema.json'
base_uri = 'https://www.example.com/schemas/'
with open(schema_filename) as schema_file:
schema = json.load(schema_file)
with open(instance_filename) as instance_file:
instance = json.load(instance_file)
resolver = jsonschema.RefResolver(base_uri=base_uri, referrer=schema)
add_local_schemas_to(resolver, schema_folder, base_uri)
jsonschema.validate(instance, schema, resolver=resolver)
{
"$id": "https://www.example.com/schemas/bar.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"bar": {
"type": "string"
}
}
}
{
"$id": "https://www.example.com/schemas/root.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"foo": {
"type": "object",
"properties": {
"bar": {
"$ref": "bar.schema.json"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment