#adapted from: https://pydantic-docs.helpmanual.io/ from typing import List from pydantic import BaseModel, Schema, BaseConfig, Json from geoalchemy2 import WKTElement from geoalchemy2.shape import to_shape import datetime as dt from pydantic import ValidationError """ now: Pydantic schemas for TOAR database """ # the following class was taken from: # https://github.com/tiangolo/fastapi/issues/312 class Coordinates(BaseModel): lat: float = Schema(0, gte=-90, lte=90) lng: float = Schema(0, gte=-180, lte=180) alt: float class StationmetaCoreBase(BaseModel): id: int codes: List[str] = [] name: str coordinates: Coordinates country: str state: str coordinate_validation_status: int coordinate_validation_date: dt.datetime type_of_environment: int type_of_area: int category: str timezone: str additional_metadata: Json coordinate_validator_id: int class Config(BaseConfig): arbitrary_types_allowed = True class StationmetaCoreCreate(StationmetaCoreBase): pass class StationmetaCore(StationmetaCoreBase): id: int class Config: orm_mode = True external_data_toar = { "id": 8, "codes":["China_test8",], "name":"Test_China", "coordinates":{"lat":36.256,"lng":17.106,"alt":1534.0}, "country":"China", "state":"Shandong Sheng", "coordinate_validation_status":0, "coordinate_validation_date":"2020-03-11T12:22:18.047974+01:00", "type_of_environment":0, "type_of_area":0, "category":"", "timezone":"", "coordinate_validator_id": 1, "additional_metadata": {} } try: stationmeta_core = StationmetaCore(**external_data_toar) print(stationmeta_core.dict()) except ValidationError as e: print(e.json())