Skip to content

Better documentation on extending RefResolver #225

Closed
@foxx

Description

@foxx

The documentation made it quite difficult to understand how RefResolver should be extended, and I had to trawl through code to make it work. This is useful when you want to reference other schemas, but you don't want to use file://

It would be nice to see better documentation on this, but in the mean time, here is how I got it working for anyone who runs into the same problem;

import jsonschema

class SchemaResolver(jsonschema.RefResolver):
    def resolve_remote(self, uri):
        if uri.startswith('/SOMETHING'):
            # your custom schema goes here
            return {}
        else:
            # fallback to original
            return super(SchemaResolver, self).resolve_remote(uri)

def validate(schema, doc):
    """Validate using custom resolver"""
    resolver = SchemaResolver.from_schema(schema)
    validator_cls = jsonschema.validators.validator_for(schema)
    validator_cls.check_schema(schema)
    validator = validator_cls(schema, resolver=resolver)
    validator.validate(doc)

schema = {"$ref": "/SOMETHING/v1/definitions.json"}
doc = {"field": "value"}

validate(schema, doc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions