Performance: Optional and nullable fields
MEDIUM IMPACT
This affects the API response size and validation speed, impacting initial response time and user experience.
from typing import Optional from pydantic import BaseModel class User(BaseModel): name: str age: Optional[int] = None # only optional if truly needed email: str
from typing import Optional from pydantic import BaseModel class User(BaseModel): name: Optional[str] = None age: Optional[int] = None email: Optional[str] = None
| Pattern | Validation Cost | Payload Size | Response Time Impact | Verdict |
|---|---|---|---|---|
| Many optional/nullable fields | High (validates many fields) | Large (includes nulls) | Slower (blocks response) | [X] Bad |
| Minimal optional/nullable fields | Low (validates fewer fields) | Small (only needed data) | Faster (quicker response) | [OK] Good |