from pydantic import BaseModel, Field # Base Schema class PlanetBase(BaseModel): name: str = Field(..., description="Name of the planet") description: str | None = Field(None, description="Description of the planet") diameter: int = Field(..., gt=0, description="Diameter of the planet in kilometers") mass: int = Field(..., gt=0, description="Mass of the planet in kilograms") distance_from_sun: int = Field(..., gt=0, description="Distance from the sun in kilometers") # Schema for creating a new Planet class PlanetCreate(PlanetBase): pass class Config: schema_extra = { "example": { "name": "Earth", "description": "The planet we live on", "diameter": 12742, "mass": 5972000000000000000000000, "distance_from_sun": 149600000 } } # Schema for Planet responses class Planet(PlanetBase): id: int class Config: orm_mode = True schema_extra = { "example": { "id": 1, "name": "Earth", "description": "The planet we live on", "diameter": 12742, "mass": 5972000000000000000000000, "distance_from_sun": 149600000 } } ``` This code defines three Pydantic schemas for the Planet entity: 1. `PlanetBase`: A base schema that defines the common fields for a planet. 2. `PlanetCreate`: A schema for creating a new planet, inheriting from `PlanetBase`. It includes an example configuration for request validation. 3. `Planet`: A schema for planet responses, inheriting from `PlanetBase` and adding an `id` field. It includes an example configuration and sets `orm_mode=True` for compatibility with ORMs like SQLAlchemy. The schemas use appropriate Pydantic field types and include validation rules using `Field()`. The `description` field is marked as nullable (`None`), while other fields are required. The `gt=0` validation ensures that numeric fields are positive.