|
1 | 1 | import logging |
2 | 2 | from typing import Optional |
3 | 3 |
|
4 | | -from pydantic import BaseModel |
| 4 | +from pydantic import BaseModel, Field, ConfigDict |
5 | 5 |
|
6 | 6 | logger = logging.getLogger(__name__) |
7 | 7 | logging.basicConfig(level=logging.INFO) |
8 | 8 | logger.setLevel(logging.INFO) |
9 | 9 |
|
| 10 | +class BaseModelWithExtra(BaseModel): |
| 11 | + model_config = ConfigDict(extra="ignore") |
10 | 12 |
|
11 | | -class GlideSetValidator(BaseModel): |
12 | | - comments: str |
13 | | - year: int |
| 13 | + |
| 14 | +class GlideSetValidator(BaseModelWithExtra): |
| 15 | + comments: Optional[str] |
| 16 | + year: int # Restricting reasonable year range |
14 | 17 | docid: int |
15 | | - latitude: float |
16 | | - homeless: int |
17 | | - source: str |
18 | | - idsource: str |
19 | | - killed: int |
20 | | - affected: int |
21 | | - duration: int |
| 18 | + latitude: float = Field(..., ge=-90, le=90) |
| 19 | + longitude: float = Field(..., ge=-180, le=180) |
| 20 | + homeless: int = Field(..., ge=0) |
| 21 | + source: Optional[str] |
| 22 | + idsource: Optional[str] |
| 23 | + killed: int = Field(..., ge=0) |
| 24 | + affected: int = Field(..., ge=0) |
| 25 | + duration: int = Field(..., ge=0) |
22 | 26 | number: str |
23 | | - injured: int |
24 | | - month: int |
| 27 | + injured: int = Field(..., ge=0) |
| 28 | + month: int = Field(..., ge=1, le=12) |
25 | 29 | geocode: str |
26 | | - location: str |
| 30 | + location: Optional[str] |
27 | 31 | magnitude: str |
28 | | - time: Optional[str] = None |
29 | | - id: Optional[str] = None |
30 | | - event: str |
31 | | - day: int |
32 | | - status: str |
33 | | - longitude: float |
| 32 | + time: Optional[str] |
| 33 | + id: Optional[str] |
| 34 | + event: str # Ensuring event is uppercase letters |
| 35 | + day: int = Field(..., ge=1, le=31) |
| 36 | + status: str |
34 | 37 |
|
35 | 38 | @classmethod |
36 | 39 | def validate_event(cls, data: dict) -> bool: |
|
0 commit comments