Skip to content

Commit faa9059

Browse files
authored
Merge pull request #22 from KNMI/temporal-validation
Add validation for number of datetimes in `Temporal.interval`
2 parents 84d6c16 + 1df0127 commit faa9059

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ classifiers = [
2222
"Typing :: Typed",
2323
]
2424
version = "0.5.0"
25-
dependencies = ["pydantic>=2.3,<3"]
25+
dependencies = ["pydantic>=2.3,<3", "typing-extensions>=4.12.2"]
2626

2727
[project.optional-dependencies]
2828
test = ["pytest", "pytest-cov"]

src/edr_pydantic/extent.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from typing import Optional
33
from typing import Union
44

5+
from annotated_types import Len
56
from pydantic import AwareDatetime
7+
from typing_extensions import Annotated
68

79
from .base_model import EdrBaseModel
810

@@ -13,8 +15,7 @@ class Spatial(EdrBaseModel):
1315

1416

1517
class Temporal(EdrBaseModel):
16-
# TODO: Validate this list has two items (C.7. Temporal Object)
17-
interval: List[List[AwareDatetime]]
18+
interval: List[Annotated[List[AwareDatetime], Len(min_length=2, max_length=2)]]
1819
# TODO: Validate this is a list of ISO 8601 single time, ISO 8601 time duration or ISO 8601 interval
1920
values: List[str]
2021
trs: str
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"interval": [
3+
[
4+
"2020-04-19T11:00:00Z"
5+
]
6+
],
7+
"values": [
8+
"2020-04-19T11:00:00Z/2020-06-30T09:00:00Z"
9+
],
10+
"trs": "TIMECRS[\"DateTime\",TDATUM[\"Gregorian Calendar\"],CS[TemporalDateTime,1],AXIS[\"Time (T)\",future]]"
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"interval": [
3+
[
4+
"2020-04-19T11:00:00Z",
5+
"2020-04-19T12:00:00Z",
6+
"2020-04-19T13:00:00Z"
7+
]
8+
],
9+
"values": [
10+
"2020-04-19T11:00:00Z/2020-06-30T09:00:00Z"
11+
],
12+
"trs": "TIMECRS[\"DateTime\",TDATUM[\"Gregorian Calendar\"],CS[TemporalDateTime,1],AXIS[\"Time (T)\",future]]"
13+
}

tests/test_edr.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from edr_pydantic.collections import Collections
88
from edr_pydantic.collections import Instance
99
from edr_pydantic.extent import Extent
10+
from edr_pydantic.extent import Temporal
1011
from edr_pydantic.parameter import Parameter
1112
from edr_pydantic.unit import Unit
1213
from pydantic import RootModel
@@ -35,7 +36,11 @@ def test_happy_cases(file_name, object_type):
3536
assert object_type.model_validate_json(json_string).model_dump_json(exclude_none=True) == json_string
3637

3738

38-
error_cases = [("label-or-symbol-unit.json", Unit, r"Either 'label' or 'symbol' should be set")]
39+
error_cases = [
40+
("label-or-symbol-unit.json", Unit, r"Either 'label' or 'symbol' should be set"),
41+
("temporal-interval-length1.json", Temporal, r"List should have at least 2 items after validation"),
42+
("temporal-interval-length3.json", Temporal, r"List should have at most 2 items after validation, not 3"),
43+
]
3944

4045

4146
@pytest.mark.parametrize("file_name, object_type, error_message", error_cases)

0 commit comments

Comments
 (0)