Search code examples
python-3.xvalidationserverfastapi

FastAPI route: Adding dynamic path parameters validation


I'm trying to add validation to my route in a fastapi server, followed the instructions here and managed to have added validation on my int path parameters like so:

route1 = router.get("/start={start:int}/end={end:int}")(route1_func)

and my route1_func:

async def route1_func(request: Request,
                      start: int = Path(..., title="my start", ge=1),
                      end: int = Path(..., title="my end", ge=1)):
    if end <= start:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
    else:
        return True

and this works great... but i would like to validate the end > start if possible as part of the definition, instead of checking this after going into route1_func

is this possible?


Solution

  • You can put path params & query params in a Pydantic class, and add the whole class as a function argument with = Depends() after it. The FastAPI docs barely mention this functionality, but it does work.

    Then, once you have your args in a Pydantic class, you can easily use Pydantic validators for custom validation.

    Here's a rough pass at your use case:

    class Route1Request(BaseModel):
        start: int = Query(..., title="my start", ge=1)
        end: int = Query(..., title="my end", ge=1)
    
        @root_validator
        def verify_start_end(cls, vals: dict) -> dict:
            assert vals.get("end", 0) > vals.get("start", 0)
            return vals
    
    @router.get("/")
    async def route1(route1_data: Route1Request = Depends()):
        return True
    

    Note: It wasn't clear to me if you meant to use path params or query params. I wrote this example for Query params, but it can work with Path params with only a few changes.