Skip to content

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

Closed
@vincentsarago

Description

@vincentsarago
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions