简体   繁体   中英

Python pydantic range validation

I have the next model:

class UserPreference(BaseModel):
    smoking: Optional[int] = Field(..., ge=0, le=4, description="How mach user = smoking", example=2)
    alcohol: Optional[int] = Field(..., ge=0, le=4, description="How mach user = alcohol", example=2)
    kids: Optional[int] = Field(..., ge=0, le=4, description="How many kids user has", example=2)
    height: Optional[int] = Field(..., ge=50, le=250, description="Height of the user", example=180)
    weight: Optional[int] = Field(..., ge=20, le=200, description="weight of the user", example=80)
    sport: Optional[int] = Field(..., ge=0, le=4, description="How mach user = sport", example=2)

    class Config:
        orm_mode = True

And I want to create a new class from the class above. Each field must be a list of 2 items (low boundary and high boundary of fields above)

A rough solution:

class UserSearchPreference(BaseModel):
    low_smoking: Optional[int] = Field(..., ge=0, le=4, description="How mach user = smoking", example=2)
    high_smoking: Optional[int] = Field(..., ge=0, le=4, description="How mach user = smoking", example=2)
    low_alcohol: Optional[int] = Field(..., ge=0, le=4, description="How mach user = alcohol", example=2)
    high_alcohol: Optional[int] = Field(..., ge=0, le=4, description="How mach user = alcohol", example=2)
    low_kids: Optional[int] = Field(..., ge=0, le=4, description="How many kids user has", example=2)
    high_kids: Optional[int] = Field(..., ge=0, le=4, description="How many kids user has", example=2)
    low_height: Optional[int] = Field(..., ge=50, le=250, description="Height of the user", example=180)
    high_height: Optional[int] = Field(..., ge=50, le=250, description="Height of the user", example=180)
    low_weight: Optional[int] = Field(..., ge=20, le=200, description="weight of the user", example=80)
    high_weight: Optional[int] = Field(..., ge=20, le=200, description="weight of the user", example=80)
    low_sport: Optional[int] = Field(..., ge=0, le=4, description="How mach user = sport", example=2)
    high_sport: Optional[int] = Field(..., ge=0, le=4, description="How mach user = sport", example=2)

How I can implement it?

PS Additionally it's preferable to check that low_boundary < high_boundary ( list[0] < list[-1] )

For your first requirement ie Each field must be a list of 2 items (low boundary and high boundary of fields above) , you can use something like this.

from typing import List

class UserSearchPreference(BaseModel):
    low_smoking: List[int] = Field(..., ge=0, le=4, min_items=2, max_items=2 description="How mach user = smoking", example=[2, 3])

As for the second requirement, you can use a custom validator or a root validator for multiple fields after parsing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM