|
8 | 8 |
|
9 | 9 | import pkg_resources |
10 | 10 | import yaml |
11 | | -from jsonschema import Draft7Validator, RefResolver |
12 | | -from jsonschema.exceptions import RefResolutionError, ValidationError |
| 11 | +from jsonschema import Draft7Validator |
| 12 | +from jsonschema.exceptions import ValidationError, RefResolutionError |
| 13 | +from referencing import Registry, Resource |
| 14 | +from referencing.jsonschema import DRAFT7 |
| 15 | +import referencing.exceptions |
13 | 16 | from nested_lookup import nested_lookup |
14 | 17 |
|
15 | 18 | from .exceptions import InternalError, SpecValidationError |
@@ -56,30 +59,33 @@ def copy_resource(package_name, resource_name, out_path): |
56 | 59 | shutil.copyfileobj(fsrc, fdst) |
57 | 60 |
|
58 | 61 |
|
59 | | -def get_schema_store(schema_search_path): |
60 | | - """Load all the schemas in schema_search_path and return a dict""" |
61 | | - schema_store = {} |
| 62 | +def get_schema_registry(schema_search_path): |
| 63 | + """Load all the schemas in schema_search_path and return a Registry""" |
| 64 | + registry = Registry() |
| 65 | + |
62 | 66 | schema_fnames = os.listdir(schema_search_path) |
63 | 67 | for schema_fname in schema_fnames: |
64 | 68 | schema_path = os.path.join(schema_search_path, schema_fname) |
65 | 69 | if schema_path.endswith(".json"): |
66 | 70 | with open(schema_path, "r", encoding="utf-8") as schema_f: |
67 | 71 | schema = json.load(schema_f) |
68 | 72 | if "$id" in schema: |
69 | | - schema_store[schema["$id"]] = schema |
70 | | - return schema_store |
| 73 | + resource = Resource.from_contents(schema, default_specification=DRAFT7) |
| 74 | + registry = registry.with_resource(schema["$id"], resource) |
| 75 | + |
| 76 | + # For JSON Schema Draft 7, also register the HTTPS version |
| 77 | + if schema["$id"] == "http://json-schema.org/draft-07/schema#": |
| 78 | + https_id = "https://json-schema.org/draft-07/schema#" |
| 79 | + registry = registry.with_resource(https_id, resource) |
| 80 | + return registry |
71 | 81 |
|
72 | 82 |
|
73 | 83 | def make_validator(schema): |
74 | 84 | schema_search_path = Path(os.path.dirname(os.path.realpath(__file__))).joinpath( |
75 | 85 | "data/schema/" |
76 | 86 | ) |
77 | | - resolver = RefResolver( |
78 | | - base_uri=Draft7Validator.ID_OF(schema), |
79 | | - store=get_schema_store(schema_search_path), |
80 | | - referrer=schema, |
81 | | - ) |
82 | | - return Draft7Validator(schema, resolver=resolver) |
| 87 | + registry = get_schema_registry(schema_search_path) |
| 88 | + return Draft7Validator(schema, registry=registry) |
83 | 89 |
|
84 | 90 |
|
85 | 91 | def make_resource_validator(): |
@@ -379,7 +385,7 @@ def load_resource_spec(resource_spec_file): # pylint: disable=R # noqa: C901 |
379 | 385 | inliner = RefInliner(base_uri, resource_spec) |
380 | 386 | try: |
381 | 387 | inlined = inliner.inline() |
382 | | - except RefResolutionError as e: |
| 388 | + except (RefResolutionError, referencing.exceptions.Unresolvable) as e: |
383 | 389 | LOG.debug("Resource spec validation failed", exc_info=True) |
384 | 390 | raise SpecValidationError(str(e)) from e |
385 | 391 |
|
@@ -444,7 +450,7 @@ def load_hook_spec(hook_spec_file): # pylint: disable=R # noqa: C901 |
444 | 450 | inliner = RefInliner(base_uri, hook_spec) |
445 | 451 | try: |
446 | 452 | inlined = inliner.inline() |
447 | | - except RefResolutionError as e: |
| 453 | + except (RefResolutionError, referencing.exceptions.Unresolvable) as e: |
448 | 454 | LOG.debug("Hook spec validation failed", exc_info=True) |
449 | 455 | raise SpecValidationError(str(e)) from e |
450 | 456 |
|
|
0 commit comments