-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add validation for ISO 8601 durations to MeasurementType.duration
#16
base: main
Are you sure you want to change the base?
Changes from all commits
1fe5eb2
672da94
0df82ed
dc93ac2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,54 @@ | ||
from datetime import timedelta | ||
from typing import Annotated | ||
from typing import Dict | ||
from typing import List | ||
from typing import Literal | ||
from typing import Optional | ||
|
||
from pydantic import AfterValidator | ||
from pydantic import model_validator | ||
from pydantic import RootModel | ||
from pydantic import TypeAdapter | ||
|
||
from .base_model import EdrBaseModel | ||
from .extent import Extent | ||
from .observed_property import ObservedProperty | ||
from .unit import Unit | ||
|
||
duration_adapter = TypeAdapter(timedelta) | ||
|
||
|
||
def check_iso8601_duration(value: str) -> str: | ||
if "/" in value: | ||
parts = value.split("/") | ||
|
||
if len(parts) != 2: | ||
raise ValueError("Duration must have two parts if it contains a '/'") | ||
|
||
duration_adapter.validate_python(parts[0]) | ||
duration_adapter.validate_python(parts[1]) | ||
else: | ||
duration_adapter.validate_python(value) | ||
|
||
return value | ||
|
||
|
||
ISO8601Duration = Annotated[str, AfterValidator(check_iso8601_duration)] | ||
|
||
|
||
class MeasurementType(EdrBaseModel): | ||
method: str | ||
# TODO: Add validation of ISO 8601 duration (including leading minus sign) | ||
# TODO: Confusion in spec on the field name, duration versus period. | ||
# See https://github.com/opengeospatial/ogcapi-environmental-data-retrieval/issues/560 | ||
period: str | ||
duration: ISO8601Duration | ||
|
||
@model_validator(mode="after") | ||
def must_have_single_duration_if_instantaneous_method(self): | ||
if self.method == "instantaneous" and "/" in self.duration: | ||
raise ValueError( | ||
"A measurement type object with 'instantaneous' method " | ||
"MUST have a single duration." | ||
) | ||
|
||
return self | ||
Comment on lines
+43
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hesitant about this additional validation logic. I can't see anything formal in the specification that says the duration must be a single duration (e.g. |
||
|
||
|
||
class Parameter(EdrBaseModel, extra="allow"): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,6 @@ | |
}, | ||
"measurementType": { | ||
"method": "instantaneous", | ||
"period": "PT0S" | ||
"duration": "PT0S" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making these improvements.
I don't think we want to keep supporting the
duration
field with a/
in there, see my comment on your other PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the
timedelta
validation that you use, I think this relies on pydantic datetime validation, correct?if I look here, it looks like this only supports part of the ISO8601 duration format:
[±]P[DD]DT[HH]H[MM]M[SS]S
I think this change would no longer allow periods like
P1M
, which is not consistent with the spec.What is your view on this?