Skip to content

Instantly share code, notes, and snippets.

@reitzig
Last active February 24, 2022 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reitzig/383509b990c5547be47e8767fe021381 to your computer and use it in GitHub Desktop.
Save reitzig/383509b990c5547be47e8767fe021381 to your computer and use it in GitHub Desktop.
jsonschema 4.4.0 RefResolver reproduction
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"shared-def": {
"type": "string",
"pattern": "^shared-"
}
}
}
{
"a": "local-a",
"b": "shared-b"
}
{
"$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"
}
}
}
{
"$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"
}
}
}
jsonschema==4.4.0
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("")
@reitzig
Copy link
Author

reitzig commented Feb 23, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment