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

Stop subclassing JSONSchema validator #1185

Merged
merged 5 commits into from
Oct 11, 2022
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Add `asdf.search.AsdfSearchResult` support for `~asdf.AsdfFile.schema_info` and
`~asdf.search.AsdfSearchResult.schema_info` method. [#1197]
- Use forc ndarray flag to correctly test for fortran array contiguity [#1206]
- Unpin ``jsonschema`` version and fix ``jsonschema`` deprecation warnings. [#1185]

2.13.0 (2022-08-19)
-------------------
Expand Down
30 changes: 22 additions & 8 deletions asdf/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from functools import lru_cache
from numbers import Integral

import attr
import numpy as np
import yaml
from jsonschema import validators as mvalidators
Expand Down Expand Up @@ -253,15 +252,25 @@ def _create_validator(validators=YAML_VALIDATORS, visit_repeat_nodes=False):
}
)
id_of = mvalidators.Draft4Validator.ID_OF
base_cls = mvalidators.create(
ASDFValidator = mvalidators.create(
meta_schema=meta_schema, validators=validators, type_checker=type_checker, id_of=id_of
)

@attr.s
class ASDFValidator(base_cls):
_context = attr.ib(factory=lambda: _ValidationContext())
ctx = attr.ib(default=None)
serialization_context = attr.ib(default=None)
def _patch_init(cls):
original_init = cls.__init__

def __init__(self, *args, **kwargs):
self.ctx = kwargs.pop("ctx", None)
self.serialization_context = kwargs.pop("serialization_context", None)

original_init(self, *args, **kwargs)

cls.__init__ = __init__

def _patch_iter_errors(cls):
original_iter_errors = cls.iter_errors

cls._context = _ValidationContext()

def iter_errors(self, instance, *args, **kwargs):
# We can't validate anything that looks like an external reference,
Expand Down Expand Up @@ -308,7 +317,12 @@ def iter_errors(self, instance, *args, **kwargs):
for val in instance:
yield from self.iter_errors(val)
else:
yield from super().iter_errors(instance)
yield from original_iter_errors(self, instance)

cls.iter_errors = iter_errors

_patch_init(ASDFValidator)
_patch_iter_errors(ASDFValidator)

return ASDFValidator

Expand Down
2 changes: 2 additions & 0 deletions asdf/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ def test_defaults():
assert t == {}


@pytest.mark.xfail(reason="This will be fixed by #1203")
def test_default_check_in_schema():
s = {"type": "object", "properties": {"a": {"type": "integer", "default": "foo"}}}

Expand All @@ -419,6 +420,7 @@ def test_default_check_in_schema():
schema.check_schema(s, validate_default=False)


@pytest.mark.xfail(reason="This will be fixed by #1203")
def test_check_complex_default():
default_software = tagged.TaggedDict({"name": "asdf", "version": "2.7.0"}, "tag:stsci.edu/asdf/core/software-1.0.0")

Expand Down
2 changes: 1 addition & 1 deletion docs/asdf/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ a schema, which can then be used to validate a file:
... type: object
... properties:
... foo:
... type: string
... type: string
... required: [foo]
... ...
... """
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
'asdf-transform-schemas >=0.2.2',
'importlib_resources >=3; python_version <"3.9"',
'jmespath >=0.6.2',
'jsonschema >=4.0.1, <4.10.0',
'jsonschema >=4.0.1',
'numpy >=1.10',
'packaging >=16.0',
'pyyaml >=3.10',
Expand Down