-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: implement validation decorator and refactor value object vali…
…dation methods
- Loading branch information
1 parent
a57fc7b
commit d8d6973
Showing
5 changed files
with
80 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
__version__ = '2025.01.03' | ||
|
||
from .decorators import validation | ||
from .models import ValueObject | ||
|
||
__all__ = ('ValueObject',) | ||
__all__ = ( | ||
'ValueObject', | ||
'validation', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .value_object_validation import validation | ||
|
||
__all__ = ('validation',) |
48 changes: 48 additions & 0 deletions
48
value_object_pattern/decorators/value_object_validation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
Validation decorator for value object pattern. | ||
""" | ||
|
||
from functools import wraps | ||
from typing import Any, Callable | ||
|
||
|
||
def validation(function: Callable[..., Any]) -> Callable[..., Any]: | ||
""" | ||
Decorator for validation. | ||
Args: | ||
function (Callable[..., Any]): Function to be execution when the value object is created. | ||
Returns: | ||
Callable[..., Any]: Wrapper function for the validation. | ||
Example: | ||
```python | ||
from value_object_pattern import ValueObject, validation | ||
class IntegerValueObject(ValueObject[int]): | ||
@validation | ||
def ensure_value_is_integer(self, value: int) -> None: | ||
if type(value) is not int: | ||
raise TypeError(f'IntegerValueObject value <<<{value}>>> must be an integer. Got <<<{type(value).__name__}>>> type.') | ||
integer = IntegerValueObject(value='invalid') | ||
# >>> TypeError: IntegerValueObject value <<<invalid>>> must be an integer. Got <<<str>>> type. | ||
``` | ||
""" # noqa: E501 # fmt: skip | ||
function._is_validation = True # type: ignore[attr-defined] | ||
|
||
@wraps(wrapped=function) | ||
def wrapper(*args: tuple[Any, ...], **kwargs: dict[str, Any]) -> None: | ||
""" | ||
Wrapper for validation. | ||
Args: | ||
*args (tuple[Any, ...]): The arguments for the function. | ||
**kwargs (dict[str, Any]): The keyword arguments for the function. | ||
""" | ||
function(*args, **kwargs) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters