I have FastAPI function like this:
@router.post("/test/")
async def test(ids: List[str] = Body(..., )):
# some logic
I want "ids" field as required and pass there values like ["1", "2"]. If I pass a valid list it working fine. But if I pass empty list - [], this is also will be valid param and I dont want that. I can write function that checks it myself pretty easy, but I feel from my expirience with this wonderful framework that FastAPI have it covered already and I just dont know how.
I finally found a solution (after about half-hour of research) - in pydantic you may pass
min_items
to indicate minimum amount of items in passing list.More on options here: https://pydantic-docs.helpmanual.io/usage/schema/#field-customisation
So in the end, pydantic + FastAPI for my case will be like this:
In this case we gain functionality of pydantic, but without need of pydantic
BaseModel
class