Skip to content
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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mo-lukecarr
Copy link
Contributor

@mo-lukecarr mo-lukecarr commented Dec 16, 2024

WIP, will move out of draft when ready for review.

Comment on lines +43 to +51
@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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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. PT0M) when the mode is instantaneous. However, this is the convention that I've seen in all EDR collections (including the JSON test data in this repository and the examples within the full EDR specification).

Comment on lines +21 to +33
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
Copy link
Collaborator

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.

Copy link
Collaborator

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?

@lukas-phaf
Copy link
Collaborator

Apologies for jumping the gun and adding some comments before the PR is ready.

@mo-lukecarr
Copy link
Contributor Author

Apologies for jumping the gun and adding some comments before the PR is ready.

No worries at all! It's all welcome feedback, and the comments raise interesting questions. Will take a proper look tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants