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

[collection-search] start/end_date are kept after model initialization #801

Open
vincentsarago opened this issue Feb 25, 2025 · 0 comments

Comments

@vincentsarago
Copy link
Member

from stac_fastapi.extensions.core.collection_search.request import BaseCollectionSearchPostRequest

BaseCollectionSearchPostRequest(datetime="2020-01-01T00:00:00Z/2020-01-02T00:00:00Z").end_date
>>> datetime.datetime(2020, 1, 2, 0, 0, tzinfo=datetime.timezone.utc)

BaseCollectionSearchPostRequest().end_date
>>> datetime.datetime(2020, 1, 2, 0, 0, tzinfo=datetime.timezone.utc)

@field_validator("datetime")
@classmethod
def validate_datetime(cls, value: str) -> str:
"""validate datetime."""
# Split on "/" and replace no value or ".." with None
values = [v if v and v != ".." else None for v in value.split("/")]
# If there are more than 2 dates, it's invalid
if len(values) > 2:
raise ValueError(
"""Invalid datetime range. Too many values.
Must match format: {begin_date}/{end_date}"""
)
# If there is only one date, duplicate to use for both start and end dates
if len(values) == 1:
values = [values[0], values[0]]
# Cast because pylance gets confused by the type adapter and annotated type
dates = cast(
List[Optional[dt]],
[
# Use the type adapter to validate the datetime strings,
# strict is necessary due to pydantic issues #8736 and #8762
SearchDatetime.validate_strings(v, strict=True) if v else None
for v in values
],
)
# If there is a start and end date,
# check that the start date is before the end date
if dates[0] and dates[1] and dates[0] > dates[1]:
raise ValueError(
"Invalid datetime range. Begin date after end date. "
"Must match format: {begin_date}/{end_date}"
)
# Store the parsed dates
cls._start_date = dates[0]
cls._end_date = dates[1]
# Return the original string value
return value

ref: stac-utils/stac-pydantic#172

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

No branches or pull requests

1 participant