-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Closed
Labels
Description
Hello!
I'm noticed a little problem with response_model feature:
Code sample:
@router.patch("/settings/swipes", response_model=schemas.UserSwipes, status_code=200)
def update_users_settings_preferences(
user_update: schemas.UserSwipes,
response: Response = Response,
token: str = Header(...),
db: Session = Depends(get_db)):
try:
...
class UserSwipes(BaseModel): # Consider UserProfile attributes as low boundary
smoking_low: Optional[int] = Field(..., ge=0, le=4, description="Minimal boundary smoking", example=2)
smoking_high: Optional[int] = Field(..., ge=0, le=4, description="Maximum boundary smoking", example=3)
alcohol_low: Optional[int] = Field(..., ge=0, le=4, description="Minimal boundary smoking", example=2)
alcohol_high: Optional[int] = Field(..., ge=0, le=4, description="Maximum boundary smoking", example=3)
@root_validator
def ensure_bigger(cls, values: dict[str]) -> dict[str]:
old_k_v = list(values.items())[0]
for k, v in values.items():
if 'low' in k:
old_k_v = k, v
else:
if k.split('_')[0] == old_k_v[0].split('_')[0]:
if old_k_v[-1] > v:
raise Exception('Minimal value biggest that Maximum item!')
else:
raise Exception("'high' item must follow 'low' item.")
return values
This @root_validator executes twice because of respomse_model and user_update: schemas.UserSwipes.
But obviously you don't need to execute a validation a second time.