Consider a FastAPI model with a field defined as name: str = Field(..., min_length=3, max_length=5). What will the API response be if a client sends {"name": "Jo"}?
from fastapi import FastAPI from pydantic import BaseModel, Field app = FastAPI() class User(BaseModel): name: str = Field(..., min_length=3, max_length=5) @app.post("/users/") async def create_user(user: User): return user
Think about how Pydantic validates fields before the endpoint logic runs.
FastAPI uses Pydantic to validate input data. If the name field is shorter than the min_length, Pydantic raises a validation error. FastAPI then returns a 422 error with details about the validation failure.
You want to ensure a field email matches a simple email pattern using FastAPI and Pydantic. Which code snippet correctly applies this validation?
Check the correct keyword argument for regex validation in Pydantic's Field.
The correct keyword is regex. Other options like pattern, match, or validate are not valid parameters for Field.
Given this FastAPI model, what will be the value of username if the client sends {} (empty JSON)?
from pydantic import BaseModel, Field, validator class User(BaseModel): username: str = Field("guest", min_length=3) @validator('username') def uppercase_username(cls, v): return v.upper()
Think about how default values and validators interact in Pydantic.
The default value "guest" meets the min_length=3 constraint. The validator converts it to uppercase, so the final value is "GUEST".
Examine the code below. Why does it raise a TypeError when trying to validate the age field?
from pydantic import BaseModel, Field class Person(BaseModel): age: int = Field(..., gt=0, lt="100")
Check the types of the parameters passed to Field.
The lt parameter expects a number, but a string "100" was given, causing a TypeError. Both gt and lt must be numbers.
Choose the correct statement about how FastAPI uses Pydantic for field validation.
Think about the order of validation and transformation in Pydantic models.
Custom validators run after Pydantic applies built-in validations and default values. They can modify or transform field values. Other options are false because defaults apply before validators, constraints are enforced during parsing, and FastAPI returns errors for empty bodies when required.