Last active
February 24, 2022 15:40
-
-
Save reitzig/383509b990c5547be47e8767fe021381 to your computer and use it in GitHub Desktop.
jsonschema 4.4.0 RefResolver reproduction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "http://json-schema.org/draft-07/schema#", | |
"type": "object", | |
"definitions": { | |
"shared-def": { | |
"type": "string", | |
"pattern": "^shared-" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"a": "local-a", | |
"b": "shared-b" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "http://json-schema.org/draft-07/schema#", | |
"type": "object", | |
"definitions": { | |
"local-def": { | |
"type": "string", | |
"pattern": "^local-" | |
} | |
}, | |
"properties": { | |
"a": { | |
"$ref": "#/definitions/local-def" | |
}, | |
"b": { | |
"$ref": "common.schema.json#/definitions/shared-def" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "http://json-schema.org/draft-07/schema#", | |
"type": "object", | |
"definitions": { | |
"local-def": { | |
"type": "string", | |
"pattern": "^local-" | |
} | |
}, | |
"properties": { | |
"a": { | |
"$ref": "example.schema.json#/definitions/local-def" | |
}, | |
"b": { | |
"$ref": "common.schema.json#/definitions/shared-def" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jsonschema==4.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import json | |
import jsonschema | |
schema_files = [ | |
Path(__file__).resolve().parent.joinpath('example.schema.json'), # _should_ work | |
Path(__file__).resolve().parent.joinpath('example_mod.schema.json') # workaround | |
] | |
json_file = Path(__file__).resolve().parent.joinpath('example.json') | |
def validator_of(base_uri: str): | |
schema = json.loads(schema_file.read_text()) | |
ref_resolver = jsonschema.RefResolver(base_uri, None) | |
validator_class = jsonschema.validators.validator_for(schema) | |
validator_class.check_schema(schema) # surprisingly, all green! $ref resolver not applied? | |
return validator_class(schema, resolver=ref_resolver) | |
for schema_file in schema_files: | |
print(schema_file) | |
base_uri_attempts = [ | |
f"{schema_file.parent.absolute().as_uri()}", # should work | |
f"{schema_file.absolute().as_uri()}", # workaround A | |
f"{schema_file.parent.absolute().as_uri()}/", # workaround B | |
] | |
for base_uri in base_uri_attempts: | |
validator = validator_of(base_uri=base_uri) | |
try: | |
validator.validate(json.loads(json_file.read_text())) | |
print(f"\t✔️\t{base_uri}") | |
except BaseException as e: | |
print(f"\t❌\t{base_uri}") | |
print(f"\t\t{e}") | |
print("") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
--> python-jsonschema/jsonschema#915 and python-jsonschema/jsonschema#916