diff --git a/CHANGELOG.md b/CHANGELOG.md index f413599..22ddefc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 \ No newline at end of file diff --git a/zon/__init__.py b/zon/__init__.py index 3a6eb58..4d346c3 100644 --- a/zon/__init__.py +++ b/zon/__init__.py @@ -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. @@ -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. @@ -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. @@ -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. @@ -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: """