I am trying to migrate an application away from jsonschema.RefResolver, due to these deprecation messages when testing:
jsonschema.RefResolver is deprecated
as of v4.18.0, in favor of the https://github.com/python-jsonschema/referencing library
Here is an example of the current code:
investigation_schema_path=os.path.join(
BASE_DIR, "..", "resources", "schemas", base_schemas_dir, "core", "investigation_schema.json"
)
with open(investigation_schema_path) as fp
investigation_schema = json.load(fp)
# Code below is uses the deprecated jsonschema.RefResolver
resolver = RefResolver("file://" + investigation_schema_path, investigation_schema)
validator = Draft4Validator(investigation_schema, resolver=resolver)
validator.validate(json_to_validate)
The code above validates all test data correctly.
Based on reading https://python-jsonschema.readthedocs.io/en/latest/referencing/ I have replaced the deprecated code with:
schema = Resource.from_contents(investigation_schema)
registry = schema @ Registry()
validator = Draft4Validator(schema, registry)
validator.validate(json_to_validate)
The result of this is that any test which should detect incorrect json_to_validate fails as no errors are reported: {'errors': [], 'warnings': [], 'validation_finished': True}
The tests which parse correct json_to_validate still appear to pass.
Presumably I have misunderstood how to use the new referencing library. Does anyone have any suggestions?
-
We need to see a complete code example including all required inputs (schema and json) and the expected output.John Gordon– John Gordon2025年10月22日 15:05:36 +00:00Commented Oct 22, 2025 at 15:05
1 Answer 1
With a bit of trial and (lack of expected) error, it turns out that this syntax will cause validation to happen correctly:
with open(schema_path) as fp:
schema = json.load(fp)
validator = Draft4Validator(schema)
validator.validate(json_in_need_of_validation)