Performance: Custom request validation
MEDIUM IMPACT
This affects the server response time and user experience by adding processing before sending responses.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.post("/items") async def create_item(item: Item): return {"message": "Item created"}
from fastapi import FastAPI, Request app = FastAPI() @app.post("/items") async def create_item(request: Request): data = await request.json() if 'name' not in data or not isinstance(data['name'], str): return {"error": "Invalid name"} # manual validation for each field if 'price' not in data or not isinstance(data['price'], (float, int)): return {"error": "Invalid price"} return {"message": "Item created"}
| Pattern | CPU Usage | Response Delay | Code Complexity | Verdict |
|---|---|---|---|---|
| Manual field checks in endpoint | High CPU per request | Delays response by ms to 10s | High, repetitive code | [X] Bad |
| Pydantic model validation | Low CPU, optimized C code | Minimal delay, async friendly | Low, declarative code | [OK] Good |