Complete the code to import the BaseModel class from Pydantic.
from pydantic import [1] class User([1]): name: str
The BaseModel class is the base for creating data models in Pydantic, which FastAPI uses for validation.
Complete the code to create a custom validator method for the 'age' field.
from pydantic import BaseModel, validator class User(BaseModel): age: int @validator('[1]') def check_age(cls, value): if value < 18: raise ValueError('Must be at least 18') return value
The validator decorator must specify the field name it validates, here 'age'.
Fix the error in the validator method name to correctly validate the 'email' field.
from pydantic import BaseModel, validator class User(BaseModel): email: str @validator('[1]') def validate_email(cls, value): if '@' not in value: raise ValueError('Invalid email') return value
The validator decorator must use the field name 'email' to apply validation on that field.
Fill both blanks to create a validator that checks the 'password' field length is at least 8 characters.
from pydantic import BaseModel, validator class User(BaseModel): password: str @validator('[1]') def [2](cls, value): if len(value) < 8: raise ValueError('Password too short') return value
The validator decorator must specify the field 'password'. The method name can be any valid name, here 'check_password' is used.
Fill all three blanks to create a validator that ensures the 'username' field is lowercase and at least 3 characters.
from pydantic import BaseModel, validator class User(BaseModel): username: str @validator('[1]') def [2](cls, value): if len(value) < [3]: raise ValueError('Username too short') return value.lower()
The validator decorator targets 'username'. The method name is 'validate_username'. The minimum length is 3 characters.