Performance: Field types and constraints
MEDIUM IMPACT
This affects the server response time and payload size by validating and serializing data efficiently before sending it to the client.
from pydantic import BaseModel, Field, EmailStr, conint class User(BaseModel): name: str = Field(..., min_length=1, max_length=50) age: conint(ge=0, le=120) # constrained int email: EmailStr # validated email
from pydantic import BaseModel class User(BaseModel): name: str age: int # no constraints email: str # no validation
| Pattern | Validation Cost | Payload Size | Server CPU Load | Verdict |
|---|---|---|---|---|
| No constraints, generic types | High (due to error handling) | Larger (unvalidated data) | High | [X] Bad |
| Specific types with constraints | Low (efficient validation) | Smaller (validated data) | Low | [OK] Good |