Skip to content

Commit

Permalink
Merge pull request #13 from Naapperas/develop
Browse files Browse the repository at this point in the history
Release 2.0.1
  • Loading branch information
Naapperas authored Jul 16, 2024
2 parents d9561a2 + b32b270 commit 8f775a1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [2.0.1] - 2024-07-16

### Changed
- Fixed the return type of `validate`

## [2.0.0] - 2024-06-20

### Added
Expand Down Expand Up @@ -50,6 +57,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added base source code files for the project.
- Base `README.md` file.

[unreleased]: https://github.com/Naapperas/zon/compare/v1.1.0...HEAD
[1.1.0]: https://github.com/Naapperas/zon/compare/v1.1.0...v1.1.0
[unreleased]: https://github.com/Naapperas/zon/compare/v2.0.1...HEAD
[2.0.1]: https://github.com/Naapperas/zon/compare/v2.0.0...v2.0.1
[2.0.0]: https://github.com/Naapperas/zon/compare/v1.1.0...v2.0.0
[1.1.0]: https://github.com/Naapperas/zon/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/Naapperas/zon/releases/tag/v1.0.0
24 changes: 16 additions & 8 deletions zon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ def _clone(self) -> Self:
return copy.deepcopy(self)

@abstractmethod
def _default_validate(self, data: T, ctx: ValidationContext):
def _default_validate(self, data: T, ctx: ValidationContext) -> T:
"""Default validation for any Zon validator
The contract for this method is the same for any other `ValidationRule`:
- If the validation succeeds, return True
- If the validation false, raise a ZonError containing the relevant data.
The contract for this method is the same for any other `ValidationRule`: If the validation fails, mark the context as dirty.
In any case, this method should return the original data
The default implementation raises a NotImplementedError.
Expand All @@ -181,14 +181,15 @@ def _default_validate(self, data: T, ctx: ValidationContext):
)

@final
def _validate(self, data: T) -> bool:
def _validate(self, data: T) -> tuple[Literal[True], T] | tuple[Literal[False], ZonError]:
"""Validates the supplied data.
Args:
data (Any): the piece of data to be validated.
Returns:
bool: True if the data is valid. This method will never return false as that case raises an error, as documented.
(bool, T) | (bool, ZonError): A tuple containing a boolean indicating whether the data is valid,
and either the validated data or a ZonError object.
Raises:
ZonError: if validation against the supplied data fails.
Expand All @@ -213,12 +214,15 @@ def _validate(self, data: T) -> bool:
return (not ctx.dirty, cloned_data if not ctx.dirty else ctx.error)

@final
def validate(self, data: T) -> bool:
def validate(self, data: T) -> T:
"""Validates the supplied data.
Args:
data (Any): the piece of data to be validated.
Returns:
T: the validated data.
Raises:
NotImplementedError: the default implementation of this method
is not implemented on base Zon class.
Expand All @@ -243,6 +247,10 @@ def safe_validate(
Args:
data (Any): the piece of data to be validated.
Returns:
(bool, T) | (bool, ZonError): A tuple containing a boolean indicating whether the data is valid,
and either the validated data or a ZonError object.
Raises:
NotImplementedError: the default implementation of this method
is not implemented on base Zon class.
Expand Down Expand Up @@ -1294,7 +1302,7 @@ def shape(self) -> Mapping[str, Zon]:
def keyof(self) -> ZonEnum:
"""Returns a validator for the keys of an object"""

return enum(self._shape.keys())
return enum(self.shape.keys())

def extend(self, extra_properties: Mapping[str, Zon]) -> Self:
"""
Expand Down

0 comments on commit 8f775a1

Please sign in to comment.