0
0
FastAPIframework~20 mins

Field validation rules in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Field Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a FastAPI endpoint receives invalid data for a field with a length constraint?

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"}?

FastAPI
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
AThe API returns a 422 Unprocessable Entity error with a message about the name being too short.
BThe API accepts the data and returns the user object with the name "Jo".
CThe API returns a 400 Bad Request error without details.
DThe API ignores the length constraints and truncates the name to 3 characters.
Attempts:
2 left
💡 Hint

Think about how Pydantic validates fields before the endpoint logic runs.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies a regex pattern validation to a FastAPI model field?

You want to ensure a field email matches a simple email pattern using FastAPI and Pydantic. Which code snippet correctly applies this validation?

Aemail: str = Field(..., regex=r"^[\w\.-]+@[\w\.-]+\.\w+$")
Bemail: str = Field(..., pattern=r"^[\w\.-]+@[\w\.-]+\.\w+$")
Cemail: str = Field(..., match=r"^[\w\.-]+@[\w\.-]+\.\w+$")
Demail: str = Field(..., validate=r"^[\w\.-]+@[\w\.-]+\.\w+$")
Attempts:
2 left
💡 Hint

Check the correct keyword argument for regex validation in Pydantic's Field.

state_output
advanced
2:00remaining
What is the value of the field after validation with a default and a custom validator?

Given this FastAPI model, what will be the value of username if the client sends {} (empty JSON)?

FastAPI
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()
ANone
B"GUEST"
CValidationError because min_length=3 is not met
D"guest"
Attempts:
2 left
💡 Hint

Think about how default values and validators interact in Pydantic.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI field validation fail with a TypeError?

Examine the code below. Why does it raise a TypeError when trying to validate the age field?

FastAPI
from pydantic import BaseModel, Field

class Person(BaseModel):
    age: int = Field(..., gt=0, lt="100")
AThe <code>age</code> field must be optional to use <code>gt</code> and <code>lt</code>.
BThe <code>gt</code> parameter cannot be used with <code>lt</code>.
CThe <code>Field</code> function does not accept <code>gt</code> or <code>lt</code> parameters.
DThe <code>lt</code> parameter must be an integer, not a string.
Attempts:
2 left
💡 Hint

Check the types of the parameters passed to Field.

🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI field validation with Pydantic is true?

Choose the correct statement about how FastAPI uses Pydantic for field validation.

AField constraints like <code>min_length</code> are enforced only at runtime inside endpoint functions.
BPydantic validators run before default values are applied to fields.
CCustom validators can modify field values after built-in validations pass.
DFastAPI ignores Pydantic field validations if the request body is empty.
Attempts:
2 left
💡 Hint

Think about the order of validation and transformation in Pydantic models.