5
5
6
6
import attr
7
7
from fastapi import Query
8
- from pydantic import BaseModel , Field , field_validator
8
+ from pydantic import BaseModel , Field , PrivateAttr , ValidationInfo , field_validator
9
9
from stac_pydantic .api .search import SearchDatetime
10
10
from stac_pydantic .shared import BBox
11
11
from typing_extensions import Annotated
@@ -64,8 +64,8 @@ class BaseCollectionSearchPostRequest(BaseModel):
64
64
65
65
# Private properties to store the parsed datetime values.
66
66
# Not part of the model schema.
67
- _start_date : Optional [dt ] = None
68
- _end_date : Optional [dt ] = None
67
+ _start_date : Optional [dt ] = PrivateAttr ( default = None )
68
+ _end_date : Optional [dt ] = PrivateAttr ( default = None )
69
69
70
70
# Properties to return the private values
71
71
@property
@@ -94,35 +94,33 @@ def validate_bbox(cls, v: BBox) -> BBox:
94
94
raise ValueError (
95
95
"Maximum elevation must greater than minimum elevation"
96
96
)
97
-
98
- if xmax < xmin :
99
- raise ValueError (
100
- "Maximum longitude must be greater than minimum longitude"
101
- )
97
+ # Validate against WGS84
98
+ if xmin < - 180 or ymin < - 90 or xmax > 180 or ymax > 90 :
99
+ raise ValueError ("Bounding box must be within (-180, -90, 180, 90)" )
102
100
103
101
if ymax < ymin :
104
102
raise ValueError (
105
103
"Maximum longitude must be greater than minimum longitude"
106
104
)
107
105
108
- # Validate against WGS84
109
- if xmin < - 180 or ymin < - 90 or xmax > 180 or ymax > 90 :
110
- raise ValueError ("Bounding box must be within (-180, -90, 180, 90)" )
111
-
112
106
return v
113
107
114
- @field_validator ("datetime" )
108
+ @field_validator ("datetime" , mode = "after" )
115
109
@classmethod
116
- def validate_datetime (cls , value : str ) -> str :
110
+ def validate_datetime (
111
+ cls , value : Optional [str ], info : ValidationInfo
112
+ ) -> Optional [str ]:
117
113
"""validate datetime."""
118
114
# Split on "/" and replace no value or ".." with None
115
+ if value is None :
116
+ return value
119
117
values = [v if v and v != ".." else None for v in value .split ("/" )]
120
118
121
119
# If there are more than 2 dates, it's invalid
122
120
if len (values ) > 2 :
123
121
raise ValueError (
124
- """Invalid datetime range. Too many values.
125
- Must match format: {begin_date}/{end_date}"""
122
+ """Invalid datetime range. Too many values. """
123
+ """ Must match format: {begin_date}/{end_date}"""
126
124
)
127
125
128
126
# If there is only one date, duplicate to use for both start and end dates
@@ -149,8 +147,8 @@ def validate_datetime(cls, value: str) -> str:
149
147
)
150
148
151
149
# Store the parsed dates
152
- cls . _start_date = dates [0 ]
153
- cls . _end_date = dates [1 ]
150
+ info . data [ " _start_date" ] = dates [0 ]
151
+ info . data [ " _end_date" ] = dates [1 ]
154
152
155
153
# Return the original string value
156
154
return value
0 commit comments