Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use jsonschema ≥4.18.0 and new referencing library #1691

Merged
merged 2 commits into from
Dec 9, 2024
Merged
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
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## __NEXT__

### Major Changes

- Drop support for older versions of jsonschema (<4.18.0). [#1691] (@victorlin)

### Bug fixes

- export: validation will no longer crash with `KeyError: 'tree'` when newer versions of jsonschema (≥4.18.0) are installed. [#1691] (@victorlin)

[#1691]: https://github.com/nextstrain/augur/pull/1691

## 26.2.0 (20 November 2024)

Expand Down
29 changes: 16 additions & 13 deletions augur/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jsonschema.exceptions
import re
from itertools import groupby
from referencing import Registry
from textwrap import indent
from typing import Iterable, Union
from augur.data import as_file
Expand Down Expand Up @@ -48,22 +49,24 @@ def load_json_schema(path, refs=None):
for k, v in refs.items():
with as_file(v) as file, open_file(file, "r") as fh:
schema_store[k] = json.load(fh)
resolver = jsonschema.RefResolver.from_schema(schema,store=schema_store)
schema_validator = Validator(schema, resolver=resolver)

# Create a dummy retrieval function to handle URIs not present in
# schema_store. This often indicates a typo (the $ref doesn't match the
# key of the schema_store) or we forgot to add a local mapping for a new
# $ref.
def retrieve(uri):
# Take advantage of the fact that BaseException is not handled by
# Registry.get_or_retrieve. This means the custom error message is
# printed instead of the less helpful default:
# jsonschema.exceptions._WrappedReferencingError: Unresolvable: https://…
raise BaseException(f"The schema used for validation could not resolve a local file for {uri!r}. " +
"Please check the schema used and update the appropriate schema_store as needed." )

registry = Registry(retrieve=retrieve).with_contents(schema_store.items())
schema_validator = Validator(schema, registry=registry)
else:
schema_validator = Validator(schema)

# By default $ref URLs which we don't define in a schema_store are fetched
# by jsonschema. This often indicates a typo (the $ref doesn't match the key
# of the schema_store) or we forgot to add a local mapping for a new $ref.
# Either way, Augur should not be accessing the network.
def resolve_remote(url):
victorlin marked this conversation as resolved.
Show resolved Hide resolved
# The exception type is not important as jsonschema will catch & re-raise as a RefResolutionError
raise Exception(f"The schema used for validation attempted to fetch the remote URL {url!r}. " +
"Augur should resolve schema references to local files, please check the schema used " +
"and update the appropriate schema_store as needed." )
schema_validator.resolver.resolve_remote = resolve_remote

return schema_validator


Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@
"cvxopt >=1.1.9, ==1.*",
"importlib_resources >=5.3.0; python_version < '3.11'",
"isodate ==0.6.*",
"jsonschema >=3.0.0, ==3.*",
"jsonschema >=4.18.0, ==4.*",
"networkx >= 2.5, <4",
"numpy ==1.*",
"packaging >=19.2",
"pandas >=1.0.0, <3",
"phylo-treetime >=0.11.2, <0.12",
"pyfastx >=1.0.0, <3.0",
"python_calamine >=0.2.0",
"referencing >=0.29.1, <1.0",
"scipy ==1.*",
"xopen[zstd] >=1.7.0, <3" # TODO: Deprecated, remove v1 support around November 2024
],
Expand Down
Loading