Skip to content

Replace registry with new instance with retrieved resource #1065

@braingram

Description

@braingram

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

No one assigned

    Labels

    EnhancementSome new desired functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions