From 5ae9eb3aee7f768c19e31e7b96bc6ec9ae778b4e Mon Sep 17 00:00:00 2001 From: Jacob Moura Date: Sat, 24 Aug 2024 12:45:00 -0300 Subject: [PATCH] added cascade mode --- CHANGELOG.md | 5 +++++ README.md | 22 ++++++++++++++++++++++ lib/src/lucid_validation.dart | 4 ++++ lib/src/validator_builder.dart | 12 ++++++++++++ pubspec.yaml | 2 +- test/mocks/mocks.dart | 3 ++- 6 files changed, 46 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6e616e..87d7903 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.0.3 + +* Added Cascade Mode + + ## 0.0.2 * Added valid CEP, CNPJ and CreditCard diff --git a/README.md b/README.md index 1e48fcb..d4de697 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,28 @@ class LoginForm extends StatelessWidget { } ``` +## Cascate Mode + +CascadeMode in LucidValidation controls the behavior of rule execution when a validation failure occurs for a property. By default, the validation rules continue to execute even if a previous rule for the same property fails. However, you can change this behavior using the CascadeMode. + +### Available Modes +`CascadeMode.continueExecution (Default)`: All validation rules for a property are executed, even if one fails. This mode is useful when you want to collect all validation errors at once. + +`CascadeMode.stopOnFirstFailure`: Stops executing further validation rules for a property as soon as a failure is detected. This is useful when you want to prevent unnecessary validation checks after an error has been found. + +You can apply CascadeMode to your validation chain using the cascaded method: + +```dart + return notEmpty() // + .minLength(5, message: 'Must be at least 8 characters long') + .mustHaveLowercase() + .mustHaveUppercase() + .mustHaveNumbers() + .mustHaveSpecialCharacter() + .cascaded(CascadeMode.stopOnFirstFailure); // change cascade mode +``` + + ## Creating Custom Rules You can easily extend the functionality of `LucidValidation` by creating your own custom rules using `extensions`. Here’s an example of how to create a validation for phone numbers: diff --git a/lib/src/lucid_validation.dart b/lib/src/lucid_validation.dart index b8e0b39..b50797e 100644 --- a/lib/src/lucid_validation.dart +++ b/lib/src/lucid_validation.dart @@ -89,12 +89,16 @@ abstract class LucidValidation { for (var propSelector in _propSelectors) { final propValue = propSelector.selector(entity); + final mode = propSelector.builder._mode; for (var rule in propSelector.builder._rules) { final result = rule(propValue); if (!result.isValid) { errors.add(result.error); + if (mode == CascadeMode.stopOnFirstFailure) { + break; + } } } } diff --git a/lib/src/validator_builder.dart b/lib/src/validator_builder.dart index b73a7e7..da448ea 100644 --- a/lib/src/validator_builder.dart +++ b/lib/src/validator_builder.dart @@ -1,5 +1,10 @@ part of 'lucid_validation.dart'; +enum CascadeMode { + continueExecution, + stopOnFirstFailure, +} + /// Builder class used to define validation rules for a specific property type [TProp]. /// /// [TProp] represents the type of the property being validated. @@ -8,6 +13,7 @@ typedef RuleFunc = ValidatorResult Function(dynamic value); class LucidValidationBuilder { final String key; final List> _rules = []; + var _mode = CascadeMode.continueExecution; /// Creates a [LucidValidationBuilder] instance with an optional [key]. /// @@ -40,4 +46,10 @@ class LucidValidationBuilder { return this; } + + /// Changes the cascade mode for this validation. + LucidValidationBuilder cascaded(CascadeMode mode) { + _mode = mode; + return this; + } } diff --git a/pubspec.yaml b/pubspec.yaml index acaab5b..9ded79b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: "A Dart/Flutter package for building strongly typed validation rule repository: https://github.com/Flutterando/lucid_validation homepage: https://pub.dev/documentation/lucid_validation/latest/ -version: 0.0.2 +version: 0.0.3 environment: sdk: ">=3.0.0 <4.0.0" diff --git a/test/mocks/mocks.dart b/test/mocks/mocks.dart index 5f8e3ef..b806e5d 100644 --- a/test/mocks/mocks.dart +++ b/test/mocks/mocks.dart @@ -50,6 +50,7 @@ extension CustomValidPasswordValidator on LucidValidationBuilder { .mustHaveLowercase() .mustHaveUppercase() .mustHaveNumbers() - .mustHaveSpecialCharacter(); + .mustHaveSpecialCharacter() + .cascaded(CascadeMode.stopOnFirstFailure); } }