Skip to content

Commit

Permalink
feat: added ISO 8601 validation for MeasurementType.duration field
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-lukecarr committed Dec 16, 2024
1 parent 672da94 commit 0df82ed
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/edr_pydantic/parameter.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
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)
duration: str
duration: ISO8601Duration


class Parameter(EdrBaseModel, extra="allow"):
Expand Down

0 comments on commit 0df82ed

Please sign in to comment.