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

Enhancement: Support configuring Pydantic's .model_validate(..., strict=True) #3572

Open
4 tasks
Anu-cool-007 opened this issue Jun 15, 2024 · 2 comments · May be fixed by #3573
Open
4 tasks

Enhancement: Support configuring Pydantic's .model_validate(..., strict=True) #3572

Anu-cool-007 opened this issue Jun 15, 2024 · 2 comments · May be fixed by #3573
Labels
Enhancement This is a new feature or request

Comments

@Anu-cool-007
Copy link

Anu-cool-007 commented Jun 15, 2024

Description

Any usage of Strict types has no effect on model validation in Litestar. This is due to the explicit strict=Fasle usage in the pydantic v2's validation method.

URL to code causing the issue

return model_type.model_validate(value, strict=False)

MCVE

from litestar import Litestar, get
from pydantic import BaseModel, StrictBool


class TestModel(BaseModel):
  test_bool: StrictBool


@post("/")
async def test_strict_model(data: TestModel) -> str:
 return "Hello, world!"


app = Litestar([hello_world])

Steps to reproduce

1. Run the attached MCVE
2. Send a POST req to the server with the following data

{
  "test_bool" : "YES"
}

3. You will receive a success response instead of an error.

Screenshots

"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

No response

Litestar Version

2.9.0
But this is a day 0 bug with Pydantic v2 support afiak.

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@Anu-cool-007 Anu-cool-007 added the Bug 🐛 This is something that is not working as expected label Jun 15, 2024
@provinzkraut provinzkraut changed the title Bug: Litestar doesn't support Strict Pydantic types Enhancement: Support configuring Pydantic's .model_validate(..., strict=True) Jun 15, 2024
@provinzkraut provinzkraut added Enhancement This is a new feature or request and removed Bug 🐛 This is something that is not working as expected labels Jun 15, 2024
@provinzkraut
Copy link
Member

Hey @Anu-cool-007, thanks for reporting this!

Although not really a bug, it makes sense to make this configurable on the Plugin. I've converted it into a feature request :)

@provinzkraut
Copy link
Member

Hey @Anu-cool-007, I've created #3573, which should address this.

With that, you can do this to get the behaviour you want:

from litestar import Litestar, get
from litestar.contrib.pydantic import PydanticInitPlugin
from pydantic import BaseModel, StrictBool


class TestModel(BaseModel):
  test_bool: StrictBool


@post("/")
async def test_strict_model(data: TestModel) -> str:
 return "Hello, world!"


app = Litestar(
    [hello_world],
    plugins=[PydanticInitPlugin(validate_strict=True)]
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement This is a new feature or request
Projects
None yet
2 participants