I'm using fastapi 0.70.0, with python 3.8.10.
I was expecting Query to raise RequestValidationError (or something) when querying /items/0 (because 0 is not gte=1)
I took the fastapi sample code, adding Query param constraint (item 0 is not allowed):
from fastapi import FastAPI, HTTPException, Query
from fastapi.exceptions import RequestValidationError
from fastapi.responses import PlainTextResponse
app = FastAPI()
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return PlainTextResponse(str(exc), status_code=400)
@app.get("/items/{item_id}")
async def read_item(item_id: int = Query(default=1, gte=1)): # /!\ Important line.
if item_id == 3:
raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
return {"item_id": item_id}
Few queries:
item 1: A valid query:
curl http://127.0.0.1:8000/items/1 # Return item_id as expected.
{"item_id": 1}
item a: Exception, "a" is not an integer:
curl http://127.0.0.1:8000/items/a # RequestValidationError as expected
item 0: No Exception raised, 0 not allowed.
curl http://127.0.0.1:8000/items/0 # ô_O, no exception, 0 is not gte=1
{"item_id": 0}
So, if no exception raised, Query constraints are not available yet ?