Skip to content

Commit

Permalink
switched from RefResolver to Registry approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Geary-Layne committed Jan 25, 2024
1 parent edce71b commit 224331d
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions python/idsse_common/idsse/common/validate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
import os
from typing import Optional, Union

from jsonschema import FormatChecker, RefResolver # pylint: disable=no-name-in-module
from jsonschema.validators import validator_for
from jsonschema import Draft202012Validator, FormatChecker
from jsonschema.protocols import Validator
from referencing import Registry
from referencing.jsonschema import DRAFT202012

# these two must (at least should) be the same draft
_validator = Draft202012Validator
_draft = DRAFT202012


def _get_refs(json_obj: Union[dict, list], result: Optional[set] = None) -> set:
Expand Down Expand Up @@ -48,23 +53,21 @@ def get_validator(schema_name) -> Validator:
schema_dir = os.path.join(current_path, 'schema')
schema_filename = os.path.join(schema_dir, schema_name+'.json')
with open(schema_filename, 'r', encoding='utf8') as file:
schema = json.load(file)

base: dict = json.loads('{"$schema": "http://json-schema.org/draft-07/schema#"}')
schema: dict = json.load(file)

dependencies = {base.get('$id'): base}
dependencies = {}
refs = _get_refs(schema)
while len(refs):
new_refs = set()
for ref in refs:
schema_filename = os.path.join(schema_dir, ref)
with open(schema_filename, 'r', encoding='utf8') as file:
ref_schema: dict = json.load(file)
dependencies[ref_schema.get('$id', ref)] = ref_schema
dependencies[ref_schema.get('$id', ref)] = _draft.create_resource(ref_schema)
new_refs = _get_refs(ref_schema, new_refs)
refs = {ref for ref in new_refs if ref not in dependencies}

resolver = RefResolver.from_schema(schema=base,
store=dependencies)

return validator_for(base)(schema, resolver=resolver, format_checker=FormatChecker())
# used default Validator type
return _validator(schema=schema,
registry=Registry().with_resources(dependencies.items()),
format_checker=FormatChecker())

0 comments on commit 224331d

Please sign in to comment.