Skip to content
Discussion options

You must be logged in to vote

You can validate it in dependency.

Also see this, may be useful for you.

from fastapi import FastAPI, Depends, Query, HTTPException
from typing import Optional

app = FastAPI()


async def validate(
    a: Optional[str] = Query(None), b: Optional[str] = Query(None)
) -> dict:
    if not a and not b:
        raise HTTPException(status_code=400, detail="must not provide both a and b")
    if a and b:
        raise HTTPException(status_code=400, detail="must provide a or b")

    return {"a": a, "b": b}


@app.get("/api")
async def func1(a_or_b: dict = Depends(validate)):
    return a_or_b

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
3 participants
Converted from issue

This discussion was converted from issue #369 on February 28, 2023 12:23.