|
8 | 8 | from datetime import datetime
|
9 | 9 | from enum import auto
|
10 | 10 | from types import DynamicClassAttribute
|
11 |
| -from typing import Any, Callable, Dict, List, Optional, Union |
| 11 | +from typing import Any, Callable, Dict, Generator, List, Optional, Union |
12 | 12 |
|
13 | 13 | import attr
|
14 | 14 | from geojson_pydantic.geometries import (
|
|
20 | 20 | Polygon,
|
21 | 21 | _GeometryBase,
|
22 | 22 | )
|
23 |
| -from pydantic import BaseModel, conint, validator |
| 23 | +from pydantic import BaseModel, ConstrainedInt, validator |
| 24 | +from pydantic.errors import NumberNotGtError |
| 25 | +from pydantic.validators import int_validator |
24 | 26 | from stac_pydantic.shared import BBox
|
25 | 27 | from stac_pydantic.utils import AutoValueEnum
|
26 | 28 |
|
|
30 | 32 | NumType = Union[float, int]
|
31 | 33 |
|
32 | 34 |
|
| 35 | +class Limit(ConstrainedInt): |
| 36 | + """An positive integer that maxes out at 10,000.""" |
| 37 | + |
| 38 | + ge: int = 1 |
| 39 | + le: int = 10_000 |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]: |
| 43 | + """Yield the relevant validators.""" |
| 44 | + yield int_validator |
| 45 | + yield cls.validate |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def validate(cls, value: int) -> int: |
| 49 | + """Validate the integer value.""" |
| 50 | + if value < cls.ge: |
| 51 | + raise NumberNotGtError(limit_value=cls.ge) |
| 52 | + if value > cls.le: |
| 53 | + return cls.le |
| 54 | + return value |
| 55 | + |
| 56 | + |
33 | 57 | class Operator(str, AutoValueEnum):
|
34 | 58 | """Defines the set of operators supported by the API."""
|
35 | 59 |
|
@@ -97,7 +121,7 @@ class BaseSearchPostRequest(BaseModel):
|
97 | 121 | Union[Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon]
|
98 | 122 | ]
|
99 | 123 | datetime: Optional[str]
|
100 |
| - limit: Optional[conint(gt=0, le=10000)] = 10 |
| 124 | + limit: Optional[Limit] = 10 |
101 | 125 |
|
102 | 126 | @property
|
103 | 127 | def start_date(self) -> Optional[datetime]:
|
|
0 commit comments