Code Monkey home page Code Monkey logo

Comments (6)

dotX12 avatar dotX12 commented on June 16, 2024

@fgsalomon, Firstly, Field should be used inside the pydantic model, not Form.

class MyRequest(BaseModel):
    a: str | None = Field(default=None)
    b: str | None = Field(default=None)

    @validator('a')
    def a_must_contain_space(cls, v):
        if not v:
            return v
        if ' ' not in v:
            raise ValueError('must contain a space')
        return v.title()
@app.post("/test")
async def test(data: MyRequest | None = FormDepends(MyRequest)):
    print(data)

image

from pyfa-converter.

dotX12 avatar dotX12 commented on June 16, 2024

Successful sending of the request, if you uncheck the swagger on send empty value buton.

from pyfa-converter.

fgsalomon avatar fgsalomon commented on June 16, 2024

@fgsalomon, Firstly, Field should be used inside the pydantic model, not Form.

class MyRequest(BaseModel):
    a: str | None = Field(default=None)
    b: str | None = Field(default=None)

Ooops, my bad. I've fixed this now. However, it seems to me it doesn't change anything.

Successful sending of the request, if you uncheck the swagger on send empty value buton.

Yeah, I know I can do that but what I would like is the model to be None when no data is sent:

curl -X 'POST' \
  'http://127.0.0.1:8000/test' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded'
"Has form data"% 

from pyfa-converter.

dotX12 avatar dotX12 commented on June 16, 2024

@fgsalomon,

class MyRequest(BaseModel):
    a: str | None = Field(default=None)
    b: str | None = Field(default=None)

    @root_validator
    def all_is_none(cls, values: dict[str, Any]):
        if all(v is None for v in values.values()):
            raise ValueError("all is none")
        return values

from pyfa-converter.

dotX12 avatar dotX12 commented on June 16, 2024

You need it?

from pyfa-converter.

dotX12 avatar dotX12 commented on June 16, 2024

i don't understand the essence of your task, first you want to allow None value, now you don't want None value...

from pyfa-converter.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.