-
|
Is it possible to add additional logic to the required query parameters? let's say we have a simple endpoint where A and B are required (in a FastAPI sense) Can we now somehow use FastAPI in a way that A or B are required (OR logic) Is there a way to handle this additional logic by FastAPI? e.g. pseudo code with overloading: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
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 |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @SlyFoxy ! 🍰 🙇♂️ Thanks @renedlog for reporting back and closing the issue 👍 |
Beta Was this translation helpful? Give feedback.
You can validate it in dependency.
Also see this, may be useful for you.