-
-
Notifications
You must be signed in to change notification settings - Fork 391
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
option to set validators of pydantic model #1471
Conversation
tortoise/contrib/pydantic/creator.py
Outdated
@@ -131,6 +131,7 @@ def pydantic_model_creator( | |||
exclude_readonly: bool = False, | |||
meta_override: Optional[Type] = None, | |||
model_config: Optional[ConfigDict] = None, | |||
__validators__: dict[str, Any] | None = None, |
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.
IMHO this should be received as validators
(without the underscores at beginning and end).
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.
Yes, I think so.
tortoise/contrib/pydantic/creator.py
Outdated
@@ -131,6 +131,7 @@ def pydantic_model_creator( | |||
exclude_readonly: bool = False, | |||
meta_override: Optional[Type] = None, | |||
model_config: Optional[ConfigDict] = None, | |||
__validators__: dict[str, Any] | None = None, |
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.
Yes, I think so.
Also please update changlog. |
Made the adjustments. Also updated the docstring. |
Just ref CHANGELOG.rst |
Is it ok? |
Thanks! |
Pull Request Test Coverage Report for Build 6756606059Warning: This coverage report may be inaccurate.We've detected an issue with your CI configuration that might affect the accuracy of this pull request's coverage report.
💛 - Coveralls |
Pydantic provides Before, After, Wrap and Plain validators, Field validators and Model validators. This is nice for cleaning and validating fields. For example, you can remove any non numeric characters for a phone field and then validate, like this:
So both phone '123-4567' and '1234567' will be validated and it will be stored as '1234567'.
The problem of tortoise "pydantic_model_creator" is that it exposes few options when calling pydantic "create_model". Also, tortoise field validators only validate data, without modifying/cleaning data.
Fortunately, you can add validators in pyndatic by passing a dict to the "validators" argument when calling "create_model".
The idea of this PR is to add "validators" option in tortoise "pydantic_model_creator" which will be used during the call of pydantic "create_model"
So in the end it would be like this: