committed by
Yurii Motov
3 changed files with 27 additions and 9 deletions
@ -1,11 +1,19 @@ |
|||
from typing import Annotated, Optional, Union |
|||
|
|||
from fastapi import FastAPI, Query |
|||
from typing import Annotated, Optional |
|||
from pydantic import BeforeValidator |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
def nullable_str(val: str) -> Union[str, None]: |
|||
if val in ("None", "", "null"): |
|||
return None |
|||
return val |
|||
|
|||
|
|||
@app.get("/items/") |
|||
async def read_items(q: Annotated[Optional[str], Query()] = ...): |
|||
if q in ("None", "", "null"): |
|||
q = None |
|||
async def read_items( |
|||
q: Annotated[Optional[str], Query(min_length=3), BeforeValidator(nullable_str)] |
|||
): |
|||
return {"q": q} |
|||
|
|||
Loading…
Reference in new issue