diff --git a/tools/schemacode/bidsschematools/schema.py b/tools/schemacode/bidsschematools/schema.py index ed6b5da5f3..a1c8860b38 100644 --- a/tools/schemacode/bidsschematools/schema.py +++ b/tools/schemacode/bidsschematools/schema.py @@ -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: @@ -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: diff --git a/tools/schemacode/bidsschematools/tests/test_schema.py b/tools/schemacode/bidsschematools/tests/test_schema.py index 6c52f0c2c1..986e7962d0 100644 --- a/tools/schemacode/bidsschematools/tests/test_schema.py +++ b/tools/schemacode/bidsschematools/tests/test_schema.py @@ -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) diff --git a/tools/schemacode/setup.cfg b/tools/schemacode/setup.cfg index f75732b593..7012ce37aa 100644 --- a/tools/schemacode/setup.cfg +++ b/tools/schemacode/setup.cfg @@ -24,7 +24,6 @@ install_requires = click pyyaml importlib_resources; python_version < "3.9" - jsonschema packages = find: include_package_data = false zip_safe = false @@ -37,6 +36,9 @@ render = tabulate pandas markdown-it-py +validation = + jsonschema + httpx expressions = pyparsing tests = @@ -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 =