Skip to content

It is working right? #11303

Discussion options

You must be logged in to vote

Reviewed this one more time.

Since v0.115.0 FastAPI officially supports Pydantic models in Query parameters (and in Header and Cookie as well).

So, after updating the code according to new docs, it works well:

from typing import Annotated
from fastapi import FastAPI, Query
from pydantic import BaseModel, field_validator


class QueryParams(BaseModel):
    test: str

    @field_validator("test")
    def check_test_starts_with(cls, v):
        if not v.startswith("test"):
            raise ValueError('O campo "test" deve começar com "test"')
        return v


app = FastAPI()


@app.get("/test")
def test(params: Annotated[QueryParams, Query()]):
    pass

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
3 participants