Complete the code to declare a query parameter named 'q' as optional.
from fastapi import FastAPI from typing import * app = FastAPI() @app.get("/items/") async def read_items(q: [1] = None): return {"q": q}
Using Optional[str] allows the query parameter to be optional (can be None or a string).
Complete the code to set a minimum length of 3 for the query parameter 'q'.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: str = Query([1], min_length=3)): return {"q": q}
The ... (Ellipsis) tells FastAPI that this query parameter is required.
Fix the error in the code to correctly validate that 'q' is an integer query parameter with a maximum value of 10.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: int = Query([1], le=10)): return {"q": q}
Using ... makes the parameter required and applies the validation with le=10 (less or equal to 10).
Fill both blanks to declare a query parameter 'q' that is optional and has a regex pattern to allow only letters.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: str = Query([1], regex=[2])): return {"q": q}
Setting the default to None makes the parameter optional. The regex "^[a-zA-Z]+$" allows only letters.
Fill all three blanks to declare a query parameter 'q' that is required, has a minimum length of 2, and a maximum length of 5.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/items/") async def read_items(q: str = Query([1], min_length=[2], max_length=[3])): return {"q": q}
The Ellipsis ... marks the parameter as required. min_length=2 and max_length=5 set length limits.