Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: partially populated nested object with required fields #51

Open
jakubczaplicki opened this issue Jun 27, 2022 · 0 comments
Open

Comments

@jakubczaplicki
Copy link

Hey all,

I am trying to understand if the behavior I am seeing is expected, a bug or misconfiguration of my setup.

The following code uses. Flask-Pydantic = "~=0.9.0"

There's an endpoint that expects the following simple payload. The address is expected to be fully populated (all fields are required).

class Address(BaseModel):
    street: str
    city: str
    region: str
    zipcode: str

class Payload(BaseModel):
    username: str
    address: Optional[Address] = None

    @root_validator
    def validate_address(cls, values):
        # address is missing from values for partially populated address fields
        if "address" not in values or not values["address"]:
            raise ValueError(f"Full address is required")
        return values

This works nicely for fully populated address and when the address is missing in the request (set to None by default).

However, when I send a partially-populated Address, the field address is missing from the payload :

Request payload :

{
"username": "foo",
"address": {
            "zipcode": "10001",
        },
    }

flask body:

{ 'username': 'foo' }

Is that expected that when a pydantic object fails to create, it will be dropped ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant