As refresolver is deprecated I tried to work around it by iterating a base scheme for all nested ref links.
In our case usually some relative path to another schema like
"properties": { "address": { "$ref": "../address/json_schema.json#/definitions/address" } },
here the very basic code which actually works fine for very small cases.
def find_refs(json_obj):
refs = []
if isinstance(json_obj, dict):
if '$ref' in json_obj:
ref = str(json_obj['$ref'])
if not ref.startswith('#'):
refs.append(json_obj['$ref'])
# Recursively check all dictionary values
for key, value in json_obj.items():
refs.extend(find_refs(value))
elif isinstance(json_obj, list):
for item in json_obj:
refs.extend(find_refs(item))
return refs
with open(schema_path) as fp:
base = json.load(fp)
base_resource = DRAFT7.create_resource(base)
draft7_resources = [('/', base_resource)]
refs = find_refs(base)
refs = set(refs)
for ref in refs:
if '#' in ref:
ref = str(ref.split('#')[0])
path = pathlib.Path(ref)
if not path.exists() and ref.startswith('..'):
path = pathlib.Path(ref.replace('..', '.'))
with open(path.__str__()) as fp:
referenced = json.load(fp)
draft7_resources.append(('/',DRAFT7.create_resource(referenced)))
registry = Registry().with_resources(draft7_resources)
validator = Draft7Validator(base, registry=registry)
and than this
result = validator.is_valid(data)
fails sometimes with _WrappedReferencingError: Unresolvable
the docs only mention direct links to http based jsonschemas.
there are quite some issues open around that question
What would be the correct way to handle nested file references in json schemas before validation ?
As refresolver is deprecated I tried to work around it by iterating a base scheme for all nested ref links.
In our case usually some relative path to another schema like
"properties": { "address": { "$ref": "../address/json_schema.json#/definitions/address" } },here the very basic code which actually works fine for very small cases.
def find_refs(json_obj): refs = [] if isinstance(json_obj, dict): if '$ref' in json_obj: ref = str(json_obj['$ref']) if not ref.startswith('#'): refs.append(json_obj['$ref']) # Recursively check all dictionary values for key, value in json_obj.items(): refs.extend(find_refs(value)) elif isinstance(json_obj, list): for item in json_obj: refs.extend(find_refs(item)) return refs with open(schema_path) as fp: base = json.load(fp) base_resource = DRAFT7.create_resource(base) draft7_resources = [('/', base_resource)] refs = find_refs(base) refs = set(refs) for ref in refs: if '#' in ref: ref = str(ref.split('#')[0]) path = pathlib.Path(ref) if not path.exists() and ref.startswith('..'): path = pathlib.Path(ref.replace('..', '.')) with open(path.__str__()) as fp: referenced = json.load(fp) draft7_resources.append(('/',DRAFT7.create_resource(referenced))) registry = Registry().with_resources(draft7_resources) validator = Draft7Validator(base, registry=registry)and than this
result = validator.is_valid(data)fails sometimes with _WrappedReferencingError: Unresolvable
the docs only mention direct links to http based jsonschemas.
there are quite some issues open around that question
What would be the correct way to handle nested file references in json schemas before validation ?