+from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException
from starlette.requests import Request
request: Request, exc: RequestValidationError
) -> JSONResponse:
return JSONResponse(
- status_code=HTTP_422_UNPROCESSABLE_ENTITY, content={"detail": exc.errors()}
+ status_code=HTTP_422_UNPROCESSABLE_ENTITY,
+ content={"detail": jsonable_encoder(exc.errors())},
)
+from decimal import Decimal
from typing import List
from fastapi import FastAPI
-from pydantic import BaseModel
+from pydantic import BaseModel, condecimal
from starlette.testclient import TestClient
app = FastAPI()
class Item(BaseModel):
name: str
- age: int
+ age: condecimal(gt=Decimal(0.0))
@app.post("/items/")
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
- "age": {"title": "Age", "type": "integer"},
+ "age": {"title": "Age", "exclusiveMinimum": 0.0, "type": "number"},
},
},
"ValidationError": {
},
}
+single_error = {
+ "detail": [
+ {
+ "ctx": {"limit_value": 0.0},
+ "loc": ["body", "item", 0, "age"],
+ "msg": "ensure this value is greater than 0",
+ "type": "value_error.number.not_gt",
+ }
+ ]
+}
+
multiple_errors = {
"detail": [
{
},
{
"loc": ["body", "item", 0, "age"],
- "msg": "value is not a valid integer",
- "type": "type_error.integer",
+ "msg": "value is not a valid decimal",
+ "type": "type_error.decimal",
},
{
"loc": ["body", "item", 1, "name"],
},
{
"loc": ["body", "item", 1, "age"],
- "msg": "value is not a valid integer",
- "type": "type_error.integer",
+ "msg": "value is not a valid decimal",
+ "type": "type_error.decimal",
},
]
}
assert response.json() == {"item": [{"name": "Foo", "age": 5}]}
-def test_put_incorrect_body():
+def test_jsonable_encoder_requiring_error():
+ response = client.post("/items/", json=[{"name": "Foo", "age": -1.0}])
+ assert response.status_code == 422
+ assert response.json() == single_error
+
+
+def test_put_incorrect_body_multiple():
response = client.post("/items/", json=[{"age": "five"}, {"age": "six"}])
assert response.status_code == 422
assert response.json() == multiple_errors