0
0
FastAPIframework~20 mins

Field types and constraints in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Field Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI endpoint with constrained fields?
Consider this FastAPI model and endpoint. What will be the response when sending {"age": 25, "name": "Anna"}?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class User(BaseModel):
    name: str = Field(min_length=3, max_length=10)
    age: int = Field(gt=18, lt=30)

@app.post("/user")
async def create_user(user: User):
    return {"message": f"User {user.name} aged {user.age} created"}
A422 Unprocessable Entity error due to name length
B422 Unprocessable Entity error due to age constraint
C{"message": "User Anna aged 25 created"}
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Check the constraints on name and age fields carefully.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a constrained string field in FastAPI?
You want a string field 'title' that must be at least 5 characters and at most 20 characters. Which code snippet is correct?
Atitle: str = Field(size=(5,20))
Btitle: str = Field(length=(5,20))
Ctitle: str = Field(min=5, max=20)
Dtitle: str = Field(min_length=5, max_length=20)
Attempts:
2 left
💡 Hint
Look for the correct parameter names for string length constraints in Pydantic Field.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI model raise a validation error for input {"score": 101}?
Given this model, why does sending {"score": 101} cause a validation error?
FastAPI
from pydantic import BaseModel, Field

class Result(BaseModel):
    score: int = Field(ge=0, le=100)
ABecause score must be a string, not an int
BBecause 101 is greater than the maximum allowed value 100
CBecause Field does not support ge and le parameters
DBecause 101 is less than the minimum allowed value 0
Attempts:
2 left
💡 Hint
Check the meaning of ge and le parameters in Field.
state_output
advanced
2:00remaining
What is the value of 'user' after parsing this input with the model?
Given this model and input, what will be the value of the 'user' variable?
FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    username: str = Field(min_length=3, max_length=8)
    active: bool = Field(default=True)

input_data = {"username": "bob"}
user = User(**input_data)
AUser(username='bob', active=True)
BUser(username='bob', active=False)
CValidationError due to missing 'active' field
DUser(username='bob') without 'active' attribute
Attempts:
2 left
💡 Hint
Check the default value of the 'active' field.
🧠 Conceptual
expert
2:30remaining
Which statement about FastAPI field constraints is TRUE?
Select the correct statement about using Field constraints in FastAPI models.
AField constraints like min_length and gt are enforced at runtime during request validation.
BField constraints only affect the OpenAPI schema but do not validate input data.
CField constraints can only be applied to string fields, not numbers.
DField constraints automatically convert invalid input to the nearest valid value.
Attempts:
2 left
💡 Hint
Think about when and how Pydantic validates data in FastAPI.