-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new plugin type for custom schema validators
- Loading branch information
Showing
25 changed files
with
526 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from asdf.extension import ManifestExtension | ||
|
||
from ._validators import ndarray | ||
|
||
VALIDATORS = [ | ||
ndarray.NdimValidator(), | ||
ndarray.MaxNdimValidator(), | ||
ndarray.DatatypeValidator(), | ||
] | ||
|
||
|
||
MANIFEST_URIS = [ | ||
"asdf://asdf-format.org/core/manifests/core-1.0.0", | ||
"asdf://asdf-format.org/core/manifests/core-1.1.0", | ||
"asdf://asdf-format.org/core/manifests/core-1.2.0", | ||
"asdf://asdf-format.org/core/manifests/core-1.3.0", | ||
"asdf://asdf-format.org/core/manifests/core-1.4.0", | ||
"asdf://asdf-format.org/core/manifests/core-1.5.0", | ||
"asdf://asdf-format.org/core/manifests/core-1.6.0", | ||
] | ||
|
||
|
||
EXTENSIONS = [ManifestExtension.from_uri(u, validators=VALIDATORS) for u in MANIFEST_URIS] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from asdf.resource import JsonschemaResourceMapping | ||
|
||
|
||
def get_extensions(): | ||
""" | ||
Get the extension instances for the core extensions. This method is registered with the | ||
asdf.extensions entry point. | ||
Returns | ||
------- | ||
list of asdf.extension.Extension | ||
""" | ||
from . import _extensions | ||
|
||
return _extensions.EXTENSIONS | ||
|
||
|
||
def get_json_schema_resource_mappings(): | ||
return [ | ||
JsonschemaResourceMapping(), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from asdf.extension import Validator | ||
from asdf.tags.core.ndarray import validate_datatype, validate_max_ndim, validate_ndim | ||
|
||
|
||
class NdimValidator(Validator): | ||
schema_property = "ndim" | ||
# The validators in this module should really only be applied | ||
# to ndarray-* tags, but that will have to be a 3.0 change. | ||
tags = ["**"] | ||
|
||
def validate(self, expected_ndim, node, schema): | ||
yield from validate_ndim(None, expected_ndim, node, schema) | ||
|
||
|
||
class MaxNdimValidator(Validator): | ||
schema_property = "max_ndim" | ||
tags = ["**"] | ||
|
||
def validate(self, max_ndim, node, schema): | ||
yield from validate_max_ndim(None, max_ndim, node, schema) | ||
|
||
|
||
class DatatypeValidator(Validator): | ||
schema_property = "datatype" | ||
tags = ["**"] | ||
|
||
def validate(self, expected_datatype, node, schema): | ||
yield from validate_datatype(None, expected_datatype, node, schema) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import pytest | ||
import yaml | ||
|
||
import asdf | ||
from asdf.core._integration import get_extensions, get_json_schema_resource_mappings | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"uri", | ||
[ | ||
"http://json-schema.org/draft-04/schema", | ||
], | ||
) | ||
def test_get_resource_mappings(uri): | ||
mappings = get_json_schema_resource_mappings() | ||
|
||
mapping = next(m for m in mappings if uri in m) | ||
assert mapping is not None | ||
|
||
assert uri.encode("utf-8") in mapping[uri] | ||
|
||
|
||
def test_get_extensions(): | ||
extensions = get_extensions() | ||
extension_uris = {e.extension_uri for e in extensions} | ||
|
||
# No duplicates | ||
assert len(extension_uris) == len(extensions) | ||
|
||
resource_extension_uris = set() | ||
resource_manager = asdf.get_config().resource_manager | ||
for resource_uri in resource_manager: | ||
if resource_uri.startswith("asdf://asdf-format.org/core/manifests/core-"): | ||
resource_extension_uris.add(yaml.safe_load(resource_manager[resource_uri])["extension_uri"]) | ||
|
||
# Make sure every core manifest has a corresponding extension | ||
assert resource_extension_uris == extension_uris |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.