Skip to content

Commit

Permalink
rf: Isolate validation-specific dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jul 15, 2024
1 parent 88fdaf8 commit 376ac03
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
17 changes: 13 additions & 4 deletions tools/schemacode/bidsschematools/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from copy import deepcopy
from functools import lru_cache

from jsonschema import ValidationError, validate

if sys.version_info < (3, 9):
from importlib_resources import files
else:
Expand Down Expand Up @@ -288,13 +286,24 @@ def filter_schema(schema, **kwargs):

def validate_schema(schema: Namespace):
"""Validate a schema against the BIDS metaschema."""
import httpx
import jsonschema
from referencing import Registry, Resource

registry: Registry = Registry(
retrieve=lambda uri: Resource.from_contents(httpx.get(uri).json())
) # type: ignore[call-arg] # Unclear why type inference fails here

metaschema = json.loads(files("bidsschematools.data").joinpath("metaschema.json").read_text())

validator_class = jsonschema.validators.validator_for(metaschema)
validator = validator_class(metaschema, registry=registry)

# validate is put in this try/except clause because the error is sometimes too long to
# print in the terminal
try:
validate(instance=schema.to_dict(), schema=metaschema)
except ValidationError as e:
validator.validate(schema.to_dict())
except jsonschema.ValidationError as e:
with tempfile.NamedTemporaryFile(
prefix="schema_error_", suffix=".txt", delete=False, mode="w+"
) as file:
Expand Down
1 change: 0 additions & 1 deletion tools/schemacode/bidsschematools/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,4 @@ def test_invalid_value():
]["density"] = "invalid"
with pytest.raises(ValidationError) as e:
schema.validate_schema(namespace)
print(e.value)
assert "invalid" in str(e.value)
7 changes: 4 additions & 3 deletions tools/schemacode/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ install_requires =
click
pyyaml
importlib_resources; python_version < "3.9"
jsonschema
packages = find:
include_package_data = false
zip_safe = false
Expand All @@ -37,6 +36,9 @@ render =
tabulate
pandas
markdown-it-py
validation =
jsonschema
httpx
expressions =
pyparsing
tests =
Expand All @@ -47,11 +49,10 @@ tests =
flake8-isort
pytest
pytest-cov
bidsschematools[render,validation,expressions]
all =
%(doc)s
%(render)s
%(tests)s
%(expressions)s

[options.package_data]
bidsschematools =
Expand Down

0 comments on commit 376ac03

Please sign in to comment.