Skip to content

Commit

Permalink
option to set validators of pydantic model (#1471)
Browse files Browse the repository at this point in the history
* option to set validators of pydantic model

* fix: adjust validators of pydantic_model_creator

* update changelog

---------

Co-authored-by: Lucas Brahm <lbrahm@rbi.com>
  • Loading branch information
lucasbrahm and lbrahm-rbi authored Nov 5, 2023
1 parent 76cfc14 commit 0ec208b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Added
^^^^^
- Add binary compression support for `UUIDField` in `MySQL`. (#1458)
- Only `Model`, `Tortoise`, `BaseDBAsyncClient`, `__version__`, and `connections` are now exported from `tortoise`
- Add parameter `validators` to `pydantic_model_creator`. (#1471)

Fixed
^^^^^
Expand Down
3 changes: 3 additions & 0 deletions tortoise/contrib/pydantic/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def pydantic_model_creator(
exclude_readonly: bool = False,
meta_override: Optional[Type] = None,
model_config: Optional[ConfigDict] = None,
validators: Optional[Dict[str, Any]] = None,
) -> Type[PydanticModel]:
"""
Function to build `Pydantic Model <https://pydantic-docs.helpmanual.io/usage/models/>`__ off Tortoise Model.
Expand All @@ -156,6 +157,7 @@ def pydantic_model_creator(
:param exclude_readonly: Build a subset model that excludes any readonly fields
:param meta_override: A PydanticMeta class to override model's values.
:param model_config: A custom config to use as pydantic config.
:param validators: A dictionary of methods that validate fields.
Note: Created pydantic model uses config_class parameter and PydanticMeta's
config_class as its Config class's bases(Only if provided!), but it
Expand Down Expand Up @@ -439,6 +441,7 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:
model = create_model(
_name,
__base__=PydanticModel,
__validators__=validators,
**properties,
)
# Copy the Model docstring over
Expand Down

0 comments on commit 0ec208b

Please sign in to comment.