r/FastAPI • u/ZpSky • 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.
10
Upvotes
View all comments
Show parent comment
3
u/Nazhmutdin2003 May 10 '25
Can you give a bit more details about your question?