High
Fastapi
Use Pydantic models for request/response
Official
Rule Description
Define explicit Pydantic models for all API inputs and outputs:
- Automatic validation and documentation
- Type safety with IDE support
- Clear API contract
```python
from pydantic import BaseModel, Field
class UserCreate(BaseModel):
email: str = Field(..., description="User email address")
name: str = Field(..., min_length=1, max_length=100)
class UserResponse(BaseModel):
id: int
email: str
name: str
created_at: datetime
class Config:
from_attributes = True
@app.post("/users", response_model=UserResponse)
async def create_user(user: UserCreate) -> UserResponse:
...
```
Add This Rule
Sign in to add this rule to your workspace
Sign in with GitHubDetails
- Severity
- High
- Category
- Fastapi
- Used in
- 4 rulesets