diff --git a/olclient/openlibrary.py b/olclient/openlibrary.py index b16ddd3d..01989d0f 100644 --- a/olclient/openlibrary.py +++ b/olclient/openlibrary.py @@ -10,6 +10,8 @@ import re from urllib.parse import urlencode from urllib.request import pathname2url +from urllib.request import pathname2url +from referencing import Registry, Resource import backoff import requests @@ -105,10 +107,12 @@ def validate(self, doc, schema_name): schemata_path = os.path.join(path, 'schemata', schema_name) with open(schemata_path) as schema_data: schema = json.load(schema_data) - resolver = jsonschema.RefResolver('file:' + pathname2url(schemata_path), schema) - return jsonschema.Draft4Validator(schema, resolver=resolver).validate( - doc.json() + base_uri = "file:" + pathname2url(schemata_path) + registry = Registry().with_resource( + base_uri, + Resource.from_contents(schema) ) + jsonschema.Draft4Validator(schema, registry=registry).validate(doc.json()) def delete(self, olid, comment): """Delete a single Open Library entity by olid (str) diff --git a/tests/schemata/test_import_schema.py b/tests/schemata/test_import_schema.py index 8402f7b8..ae3f0e22 100644 --- a/tests/schemata/test_import_schema.py +++ b/tests/schemata/test_import_schema.py @@ -3,6 +3,8 @@ import jsonschema import os import pytest +from urllib.request import pathname2url +from referencing import Registry, Resource IMPORT_SCHEMA = os.path.join( os.path.dirname(__file__), '..', '..', 'olclient', 'schemata', 'import.schema.json' @@ -86,11 +88,19 @@ }, ] - @pytest.mark.parametrize('example', examples) def test_import_examples(example): - with open(IMPORT_SCHEMA) as schema_data: + with open(IMPORT_SCHEMA, encoding="utf-8") as schema_data: schema = json.load(schema_data) - resolver = jsonschema.RefResolver('file:' + pathname2url(IMPORT_SCHEMA), schema) - result = jsonschema.Draft4Validator(schema, resolver=resolver).validate(example) - assert result is None + + base_uri = "file:" + pathname2url(IMPORT_SCHEMA) + + # Create a registry with the schema at the base URI + registry = Registry().with_resource( + base_uri, + Resource.from_contents(schema) + ) + + # Validate using the registry instead of RefResolver + result = jsonschema.Draft4Validator(schema, registry=registry).validate(example) + assert result is None \ No newline at end of file