0
0
FastAPIframework~20 mins

Query parameter validation in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Parameter Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing the endpoint with ?q=fastapi123?

Consider this FastAPI endpoint that validates a query parameter q to be a string with a minimum length of 5 and maximum length of 10:

from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: str = Query(..., min_length=5, max_length=10)):
    return {"q": q}

What will be the response when a client requests /items/?q=fastapi123?

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/items/")
async def read_items(q: str = Query(..., min_length=5, max_length=10)):
    return {"q": q}
A422 Unprocessable Entity error due to length exceeding max_length
B500 Internal Server Error
C{"q": "fastapi"}
D{"q": "fastapi123"}
Attempts:
2 left
💡 Hint

Check the length of the string fastapi123 and compare it with the min and max length constraints.

📝 Syntax
intermediate
2:00remaining
Which option correctly validates an optional integer query parameter with a default of 10 and a range 1 to 100?

Choose the correct FastAPI query parameter declaration for an optional integer count with default 10, minimum 1, and maximum 100.

Acount: int = Query(default=10, min_length=1, max_length=100)
Bcount: int = Query(10, ge=1, le=100)
Ccount: int = Query(10, min=1, max=100)
Dcount: Optional[int] = Query(default=10, min=1, max=100)
Attempts:
2 left
💡 Hint

Remember that ge and le are used for numeric bounds, and min_length and max_length are for strings.

state_output
advanced
2:00remaining
What is the response when calling /search/?tags=python&tags=fastapi&tags=api?

Given this FastAPI endpoint:

from fastapi import FastAPI, Query
from typing import List

app = FastAPI()

@app.get("/search/")
async def search(tags: List[str] = Query([])):
    return {"tags": tags}

What will be the JSON response when the client calls /search/?tags=python&tags=fastapi&tags=api?

FastAPI
from fastapi import FastAPI, Query
from typing import List

app = FastAPI()

@app.get("/search/")
async def search(tags: List[str] = Query([])):
    return {"tags": tags}
A{"tags": ["python", "fastapi", "api"]}
B{"tags": "python,fastapi,api"}
C422 Unprocessable Entity error
D{"tags": ["python"]}
Attempts:
2 left
💡 Hint

Think about how FastAPI handles multiple query parameters with the same name when using a List type.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a validation error when called with ?age=twenty?

Examine this FastAPI endpoint:

from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/users/")
async def get_user(age: int = Query(..., ge=0, le=120)):
    return {"age": age}

What is the reason for a 422 validation error when the client calls /users/?age=twenty?

FastAPI
from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/users/")
async def get_user(age: int = Query(..., ge=0, le=120)):
    return {"age": age}
AThe string 'twenty' cannot be converted to an integer, causing a validation error.
BThe age value is below the minimum allowed (0).
CThe age value exceeds the maximum allowed (120).
DThe Query parameter is missing a default value.
Attempts:
2 left
💡 Hint

Consider the data type expected and the actual input value.

🧠 Conceptual
expert
3:00remaining
Which option best describes how FastAPI validates query parameters with Pydantic constraints?

FastAPI uses Pydantic models and field constraints to validate query parameters. Which statement correctly explains this process?

AFastAPI validates query parameters only after the endpoint function runs, raising errors inside the function.
BFastAPI only checks the type of query parameters but ignores any additional constraints like min_length or ge.
CFastAPI converts query parameters to the declared type, then applies Pydantic validators and constraints, returning errors if validation fails.
DFastAPI requires manual validation code inside the endpoint to enforce constraints on query parameters.
Attempts:
2 left
💡 Hint

Think about when and how FastAPI applies validation rules declared with Query and Pydantic.