|
12 | 12 | from shapely.geometry import shape
|
13 | 13 |
|
14 | 14 | from geojson_pydantic.geometries import Polygon
|
15 |
| -from pydantic import Field, root_validator |
| 15 | +from pydantic import Field, ValidationError, root_validator |
| 16 | +from pydantic.error_wrappers import ErrorWrapper |
16 | 17 | from stac_api import config
|
17 | 18 | from stac_api.models.decompose import CollectionGetter, ItemGetter
|
18 | 19 | from stac_pydantic import Collection as CollectionBase
|
@@ -177,12 +178,30 @@ class STACSearch(Search):
|
177 | 178 | query: Optional[Dict[Queryables, Dict[Operator, Any]]]
|
178 | 179 | token: Optional[str] = None
|
179 | 180 |
|
| 181 | + @root_validator(pre=True) |
| 182 | + def validate_query_fields(cls, values: Dict) -> Dict: |
| 183 | + """validate query fields""" |
| 184 | + if "query" in values and values["query"]: |
| 185 | + queryable_fields = Queryables.__members__.values() |
| 186 | + for field_name in values["query"]: |
| 187 | + if field_name not in queryable_fields: |
| 188 | + raise ValidationError( |
| 189 | + [ |
| 190 | + ErrorWrapper( |
| 191 | + ValueError(f"Cannot search on field: {field_name}"), |
| 192 | + "STACSearch", |
| 193 | + ) |
| 194 | + ], |
| 195 | + STACSearch, |
| 196 | + ) |
| 197 | + return values |
| 198 | + |
180 | 199 | @root_validator
|
181 | 200 | def include_query_fields(cls, values: Dict) -> Dict:
|
182 | 201 | """
|
183 | 202 | Root validator to ensure query fields are included in the API response
|
184 | 203 | """
|
185 |
| - if values["query"]: |
| 204 | + if "query" in values and values["query"]: |
186 | 205 | query_include = set(
|
187 | 206 | [
|
188 | 207 | k.value
|
|
0 commit comments