fast api Flashcards
conint
from pydantic import conint
class ExampleModel(BaseModel):
age: conint(gt=18, le=99) # age must be between 19 and 99 inclusive
constr
from pydantic import BaseModel, constr
class ExampleModel(BaseModel):
name: constr(min_length=3, max_length=50, regex=’^[a-zA-Z ]+$’) # name must be between 3 and 50 characters, and only contain letters and spaces
conlist
from pydantic import BaseModel, conlist
class ExampleModel(BaseModel):
tags: conlist(str, min_items=1, max_items=5) # tags must be a list of strings with 1 to 5 items
confloat
from pydantic import BaseModel, confloat
class ExampleModel(BaseModel):
price: confloat(ge=0.0) # price must be a non-negative float
condecimal
from pydantic import BaseModel, condecimal
from decimal import Decimal
class ExampleModel(BaseModel):
balance: condecimal(gt=Decimal(‘0.0’), max_digits=10, decimal_places=2) # balance must be a positive decimal with up to 10 digits and 2 decimal places