-
-
Notifications
You must be signed in to change notification settings - Fork 790
Fix json validation of complex types in strict models #818
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
base: main
Are you sure you want to change the base?
Conversation
59b33c8 to
93acb8c
Compare
93acb8c to
6b4c652
Compare
| setattr(new_obj, key, value) | ||
| return new_obj | ||
|
|
||
| def sqlmodel_validate_python( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it me or is this redefined later in this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is wrapped inside an if else block for pydantic v1 vs v2 at line 72.
|
Hi @JacobHayes, This PR has been inactive for some time. Are you still able to work on it? If not, please let us know so we can continue the development from this point. @tiangolo What is your perspective on this? Maybe you have an idea regarding the TypeAdapter. |
|
I'm not quite sure what to look into next and external PRs don't seem to be too prioritized (no judgement there, I know it takes a lot of work 😃), so I personally won't spend much more time on this. The PR should be open to edits by maintainers or I'm happy to close if you all prefer. |
|
This pull request has a merge conflict that needs to be resolved. |
I'd like to use SQLModel with strict models exposed via FastAPI. However, there seem to be a few edge cases specifically with date, UUID, and similar "complex" types that aren't native json types where the "strict validate json" behavior is not used and instead SQLModel/FastAPI/Pydantic is expecting the exact type (eg: a
dateorUUIDinstance) which obviously fails when we're parsing from a json string. For example, this script:errors for the
SQLStrictmodel onmaincallingmodel_validate_jsonandTypeAdapter.validate_jsonwhile all of the other models work as expected.This PR implements
SQLModel.model_validate_jsonby splittingsqlmodel_validateinto a main helper with*_pythonand*_jsonvariants that defer to the appropriate Pydantic method.This fixes the direct
.model_validate_jsoncalls, but there still seem to be some separate issues with the TypeAdapter. I'm not sure yet what's wrong with TypeAdaptor aroundSQLModels (given it works fine withPydanticStrictabove), but this seems to be the main blocker for fixing the usage in FastAPI so I've kept this PR as a draft for now while I figure that out - I'd appreciate any guidance!