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

Fix validation against dataclasses #212

Merged
merged 4 commits into from
Mar 15, 2024
Merged

Fix validation against dataclasses #212

merged 4 commits into from
Mar 15, 2024

Conversation

DanielRosenwasser
Copy link
Member

In #201 and #208, I switched us over from using Pydantic's validate_json to validate_python. However, there's a subtlety. From the docs:

It also handles constructing the correct Python type even in strict mode, where validate_python(json.loads(json_data)) would fail validation.

That might sound surprising if you don't catch what happens at each step. The subtlety here is that by the time data is loaded up from json.loads, it comes in as a dict. Under strict=True, that dict is not compatible with any type other than certain kinds of dicts and TypedDicts. So

Pydantic already has issues at pydantic/pydantic-core#712 and pydantic/pydantic#9009 to enable the same flexibilities that validate_json has, but for now the recommended workaround is to reserialize and use validate_json.

@DanielRosenwasser DanielRosenwasser merged commit 79cac6b into main Mar 15, 2024
8 checks passed
@DanielRosenwasser DanielRosenwasser deleted the roundTripJson branch March 15, 2024 19:29
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

Successfully merging this pull request may close these issues.

None yet

1 participant