Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions olclient/openlibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 15 additions & 5 deletions tests/schemata/test_import_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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