-
-
Notifications
You must be signed in to change notification settings - Fork 595
Closed
Labels
EnhancementSome new desired functionalitySome new desired functionality
Description
Thanks in advance for your help with this.
Is there a way for a validator to replace the registry with a new instance that includes a retrieved resource (to avoid multiple retrievals for the same uri)?
Running the following:
import jsonschema
import referencing
uri = "https://somewhere.org/foo/bar"
obj_schema = {
"$id": uri,
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
},
}
cart_schema = {
"properties": {
"foo": {
"$ref": uri,
},
"bar": {
"$ref": uri,
},
},
}
cart = {"foo": {"price": 1}, "bar": {"price": 2}}
# make a validator with a registry
def retrieve(target_uri):
if target_uri == uri:
resource = referencing.Resource(contents=obj_schema, specification=referencing.jsonschema.DRAFT4)
print(f"{target_uri} retrieved")
return resource
registry = referencing.Registry(retrieve=retrieve)
validator = jsonschema.validators.Draft4Validator(cart_schema, registry=registry)
validator.validate(cart)
Results in the follow output:
https://somewhere.org/foo/bar retrieved
https://somewhere.org/foo/bar retrieved
due to retrieve
being called twice.
Metadata
Metadata
Assignees
Labels
EnhancementSome new desired functionalitySome new desired functionality