r/FastAPI • • May 10 '25

Question Schema validation best practices

Howdy, FastAPI pro-s! Please share your wisdom, what is the best option to describe request\response schemas?

I want to declare schemas once in separate schemas.py, and use it for database fetching, fastapi requests, response, documentation in OpenAPI, etc.

But my struggle is that I see multiple options:

  • Pydantic Field: `precise: Decimal = Field(max_digits=5, decimal_places=2)`
  • Pydantic types: `year: PositiveInt`
  • Annotations: `description: Annotated[Union[str, None], Field(title="The description of the item", max_length=300)]`
  • FastAPI types: `name: Query(description="...", min_length=1, max_length=64),`

What is the modern and supported way to write code? I've checked multiple sources, including FastAPI documentation but there's no answer to that unfortunately.

9 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/Firm_Scheme728 Jul 11 '25
# other base file
class ListCommand(BaseCommand):
    async def execute(self):
        query = await self.build_query()

        items, total = await self.paginate(query)

        self.session.expunge_all()

        items = await self.injects(items)

        return self.build_response(await self.format_items(items), total)

    async def build_query(self):
        return await self.repo.build_query(request=self.request, filter_class=self.filter_class, **self.default_get_instance_kw)

    async def paginate(self, query) -> Tuple[List[dict], int]:
        return await self.filter_class.paginate(session=self.session, query=query)

    async def format_items(self, items: List[dict]) -> List[BaseModel]:
        return [self.response_schema.model_validate(item, from_attributes=True) for item in items]

    def build_response(self, items: List[BaseModel], total: int):
        return self.filter_class.build_page(items=items, total=total)

    async def injects(self, items):
        return items