Skip to content

Allow for caching of the jsonschema RefResolver #4

@jhosteny

Description

@jhosteny

Hi @jbwyme, this package is working really nicely for me. One problem I've had is that the jsonschema RefResolver is recreated for each validation, and thus it pulls external $ref's when they could otherwise be cached.

I have a change that fixes this by creating and caching one when the schema is looked up. For example, in Resolver.lookup_schema:

ref_resolver = jsonschema.RefResolver.from_schema(lookup_result)
self.cache[schema_path] = [lookup_result, ref_resolver, store_time]
return lookup_result, ref_resolver

Then that is passed to validation like this:

schema, ref_resolver = resolver.lookup_schema(self.schema_key)
jsonschema.validate(
        instance=self.data,
        schema=schema,
        resolver=ref_resolver,
        format_checker=jsonschema.FormatChecker(),
)

I'm not sure if this is the proper way to do this, so I didn't submit a PR yet. Is there perhaps an easier way to do this that you are aware of?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions