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 fastapi import FastAPI, Query |
||||
from typing import Annotated, Optional |
from pydantic import BeforeValidator |
||||
|
|
||||
app = FastAPI() |
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
def nullable_str(val: str) -> Union[str, None]: |
||||
|
if val in ("None", "", "null"): |
||||
|
return None |
||||
|
return val |
||||
|
|
||||
|
|
||||
@app.get("/items/") |
@app.get("/items/") |
||||
async def read_items(q: Annotated[Optional[str], Query()] = ...): |
async def read_items( |
||||
if q in ("None", "", "null"): |
q: Annotated[Optional[str], Query(min_length=3), BeforeValidator(nullable_str)] |
||||
q = None |
): |
||||
return {"q": q} |
return {"q": q} |
||||
|
|||||
Loading…
Reference in new issue