Skip to content

Commit

Permalink
updated jsonschema validator and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
evalott100 committed Oct 1, 2024
1 parent 94c7801 commit 3ad6e1d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
8 changes: 5 additions & 3 deletions event_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1801,18 +1801,20 @@ class MismatchedDataKeys(InvalidData):

def _is_array(checker, instance):
return (
jsonschema.validators.Draft7Validator.TYPE_CHECKER.is_type(instance, "array")
jsonschema.validators.Draft202012Validator.TYPE_CHECKER.is_type(
instance, "array"
)
or isinstance(instance, tuple)
or hasattr(instance, "__array__")
)


_array_type_checker = jsonschema.validators.Draft7Validator.TYPE_CHECKER.redefine(
_array_type_checker = jsonschema.validators.Draft202012Validator.TYPE_CHECKER.redefine(
"array", _is_array
)

_Validator = jsonschema.validators.extend(
jsonschema.validators.Draft7Validator, type_checker=_array_type_checker
jsonschema.validators.Draft202012Validator, type_checker=_array_type_checker
)

schema_validators = {
Expand Down
15 changes: 6 additions & 9 deletions event_model/documents/event_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ class Limits(TypedDict):
warning: NotRequired[Annotated[LimitsRange, Field(description="Warning limits.")]]
alarm: NotRequired[Annotated[LimitsRange, Field(description="Alarm limits.")]]


_ConstrainedDtype = Annotated[
str,
Field(
description="A numpy dtype e.g `<U9`, `<f16`",
pattern="[|<>][tbiufcmMOSUV][0-9]+"
)
pattern="[|<>][tbiufcmMOSUV][0-9]+",
),
]

_ConstrainedDtypeNpStructure = Tuple[str, _ConstrainedDtype]


class DataKey(TypedDict):
"""Describes the objects in the data property of Event documents"""

Expand All @@ -56,16 +58,11 @@ class DataKey(TypedDict):
"The type of the data in the event, given as a broad "
"JSON schema type."
)
)
),
]
dtype_numpy: NotRequired[
Annotated[
Union[
_ConstrainedDtype,
List[
_ConstrainedDtypeNpStructure
]
],
Union[_ConstrainedDtype, List[_ConstrainedDtypeNpStructure]],
Field(
description=(
"The type of the data in the event, given as a "
Expand Down
12 changes: 7 additions & 5 deletions event_model/schemas/event_descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
},
"dtype_numpy": {
"title": "Dtype Numpy",
"description": "The type of the data in the event, given as a numpy dtype string (or, for structured dtypes, array).",
"description": [
"The type of the data in the event, given as a numpy dtype string (or, for structured dtypes, array)."
],
"anyOf": [
{
"description": "A numpy dtype e.g `<U9`, `<f16`",
Expand All @@ -71,8 +73,6 @@
},
{
"items": {
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"type": "string"
Expand All @@ -83,9 +83,11 @@
"type": "string"
}
],
"type": "array"
"type": "array",
"unevaluatedItems": false
},
"type": "array"
"type": "array",
"unevaluatedItems": false
}
]
},
Expand Down
55 changes: 44 additions & 11 deletions event_model/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,59 @@ def test_dots_not_allowed_in_keys():


@pytest.mark.parametrize(
"dtype_numpy", [
"Z",
"i",
"i4",
"4i",
"i>4",
">i",
[("some_str", "Z")],
]
"dtype_numpy",
[
"Z",
"i",
"i4",
"4i",
"i>4",
">i",
("some_str_1", "<u4"),
[("some_str_1", "<u4"), ("some_str", "Z")],
],
)
def test_bad_numpy_datakeys(dtype_numpy):
descriptor = {
"time": 0,
"uid": new_uid(),
"data_keys": {
"a": {
"source": "", "dtype": "number", "shape": [], "dtype_numpy": dtype_numpy
"source": "",
"dtype": "number",
"shape": [],
"dtype_numpy": dtype_numpy,
}
},
"run_start": new_uid(),
}
with pytest.raises(jsonschema.ValidationError):
with pytest.raises(
jsonschema.ValidationError,
):
schema_validators[DocumentNames.descriptor].validate(descriptor)


@pytest.mark.parametrize(
"dtype_numpy",
[
">u4",
"<u4",
[("some_str_1", "<u4"), ("some_str", "<u4")],
[("some_str_1", "<u4"), ("some_str", ">u4")],
],
)
def test_good_numpy_datakeys(dtype_numpy):
descriptor = {
"time": 0,
"uid": new_uid(),
"data_keys": {
"a": {
"source": "",
"dtype": "number",
"shape": [],
"dtype_numpy": dtype_numpy,
}
},
"run_start": new_uid(),
}
schema_validators[DocumentNames.descriptor].validate(descriptor)

0 comments on commit 3ad6e1d

Please sign in to comment.