From 1f98a8875cbaa9dea8de4204a01d76d8984cb25b Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 13:05:09 -0500 Subject: [PATCH] Add Planet schema --- schemas/planet.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 schemas/planet.py diff --git a/schemas/planet.py b/schemas/planet.py new file mode 100644 index 0000000..939dcb0 --- /dev/null +++ b/schemas/planet.py @@ -0,0 +1,50 @@ +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. \ No newline at end of file