Skip to content

Commit

Permalink
added cascade mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobaraujo7 committed Aug 24, 2024
1 parent 18d3540 commit 5ae9eb3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.3

* Added Cascade Mode


## 0.0.2

* Added valid CEP, CNPJ and CreditCard
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions lib/src/lucid_validation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ abstract class LucidValidation<E> {

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;
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/src/validator_builder.dart
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -8,6 +13,7 @@ typedef RuleFunc<TProp> = ValidatorResult Function(dynamic value);
class LucidValidationBuilder<TProp> {
final String key;
final List<RuleFunc<TProp>> _rules = [];
var _mode = CascadeMode.continueExecution;

/// Creates a [LucidValidationBuilder] instance with an optional [key].
///
Expand Down Expand Up @@ -40,4 +46,10 @@ class LucidValidationBuilder<TProp> {

return this;
}

/// Changes the cascade mode for this validation.
LucidValidationBuilder<TProp> cascaded(CascadeMode mode) {
_mode = mode;
return this;
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion test/mocks/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extension CustomValidPasswordValidator on LucidValidationBuilder<String> {
.mustHaveLowercase()
.mustHaveUppercase()
.mustHaveNumbers()
.mustHaveSpecialCharacter();
.mustHaveSpecialCharacter()
.cascaded(CascadeMode.stopOnFirstFailure);
}
}

0 comments on commit 5ae9eb3

Please sign in to comment.