From 6f8229d1f896de74431bcbcc985e9f5ba7584950 Mon Sep 17 00:00:00 2001 From: Nuno Pereira Date: Tue, 16 Jul 2024 11:13:14 +0100 Subject: [PATCH 1/2] Fixed validate return type --- CHANGELOG.md | 5 +++++ zon/__init__.py | 24 ++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f413599..96c0fc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ 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] + +### Changed +- Fixed the return type of `validate` + ## [2.0.0] - 2024-06-20 ### Added 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: """ From b32b270dda976454cbec4853430846d90c7b254b Mon Sep 17 00:00:00 2001 From: Nuno Pereira Date: Tue, 16 Jul 2024 11:23:17 +0100 Subject: [PATCH 2/2] Prepare new release --- CHANGELOG.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96c0fc3..22ddefc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.0.1] - 2024-07-16 + ### Changed - Fixed the return type of `validate` @@ -55,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