0
0
FastAPIframework~20 mins

String validation (min, max, regex) in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Validation Mastery
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 a string shorter than the minimum length?

Consider this FastAPI model using Pydantic for validation:

from pydantic import BaseModel, constr

class User(BaseModel):
    username: constr(min_length=5, max_length=10)

@app.post('/user')
async def create_user(user: User):
    return {'username': user.username}

If a client sends {"username": "abc"}, what will FastAPI do?

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel, constr

app = FastAPI()

class User(BaseModel):
    username: constr(min_length=5, max_length=10)

@app.post('/user')
async def create_user(user: User):
    return {'username': user.username}
AAccept the input and return the username as is
BReturn a 400 Bad Request error with a generic message
CIgnore the username field and return an empty string
DReturn a 422 Unprocessable Entity error because the string is too short
Attempts:
2 left
💡 Hint

Think about how Pydantic validates string length constraints.

📝 Syntax
intermediate
2:00remaining
Which Pydantic field declaration correctly enforces a regex pattern for a string?

Choose the correct way to declare a Pydantic model field that only accepts strings matching the pattern ^[a-z]{3}\d{2}$ (three lowercase letters followed by two digits).

Acode: username: constr(regex='^[a-z]{3}\d{2}$')
Bcode: username: str(regex='^[a-z]{3}\d{2}$')
Ccode: username: constr(pattern='^[a-z]{3}\d{2}$')
Dcode: username: str(pattern='^[a-z]{3}\d{2}$')
Attempts:
2 left
💡 Hint

Remember that constr is used for constrained strings in Pydantic.

state_output
advanced
2:00remaining
What is the response when a string exceeds the max_length in a FastAPI request?

Given this Pydantic model:

class Item(BaseModel):
    code: constr(min_length=2, max_length=4)

@app.post('/items')
async def create_item(item: Item):
    return {'code': item.code}

If the client sends {"code": "ABCDE"}, what will be the HTTP status code and response?

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel, constr

app = FastAPI()

class Item(BaseModel):
    code: constr(min_length=2, max_length=4)

@app.post('/items')
async def create_item(item: Item):
    return {'code': item.code}
A400 Bad Request with a generic error message
B200 OK with the code returned as "ABCDE"
C422 Unprocessable Entity with validation error about max_length
D500 Internal Server Error due to validation failure
Attempts:
2 left
💡 Hint

Consider how Pydantic handles max_length violations.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint accept invalid strings despite constraints?

Examine this code snippet:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Data(BaseModel):
    code: str

@app.post('/data')
async def post_data(data: Data):
    return {'code': data.code}

The developer wants to enforce min_length=3 and a regex pattern ^[A-Z]+$ on code, but the endpoint accepts any string. Why?

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Data(BaseModel):
    code: str

@app.post('/data')
async def post_data(data: Data):
    return {'code': data.code}
AThe field uses plain str type without constraints, so no validation is applied
BFastAPI does not support string validation on POST requests
CThe regex pattern must be applied in the endpoint function, not the model
DThe code field is missing a default value, so validation is skipped
Attempts:
2 left
💡 Hint

Think about how Pydantic enforces validation rules.

🧠 Conceptual
expert
3:00remaining
How does FastAPI handle regex validation errors internally when using Pydantic's constr?

When a FastAPI endpoint receives a string that fails the regex validation defined by constr(regex=...), what is the internal process FastAPI follows before returning a response?

AFastAPI ignores the regex failure and returns the string as is
BPydantic raises a ValidationError, FastAPI catches it and returns a 422 response with error details
CFastAPI raises a 500 Internal Server Error due to unhandled exception
DThe regex validation is performed by FastAPI middleware, which returns a 400 error on failure
Attempts:
2 left
💡 Hint

Recall how FastAPI integrates with Pydantic for validation.