Skip to content

Commit

Permalink
feature: aadded overrideCallback in byField
Browse files Browse the repository at this point in the history
  • Loading branch information
wellgenio committed Jan 23, 2025
1 parent 57778e7 commit 8075437
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.3

* Added overrideCallback in byField

## 1.2.2

* Added validCPFOrCNPJ
Expand Down
37 changes: 32 additions & 5 deletions lib/src/lucid_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,36 @@ abstract class LucidValidator<E> {
/// Example:
/// ```dart
///
/// void callback(errors) {}
///
/// final validator = UserValidation();
/// final emailValidator = validator.byField(user, 'email', callback);
/// final emailValidator = validator.byField(user, 'email');
/// String? validationResult = emailValidator('[email protected]');
/// ```
String? Function([String?]) byField(E entity, String key, [Function(List<ValidationException>)? callback]) {
///
/// or
/// ```dart
///
/// void callback (errors) {}
///
/// final validator = UserValidation();
/// final emailValidator = validator.byField(user, 'email', overrideCallback: callback);
/// emailValidator('[email protected]'); // return null when overrideCallback is not null
/// ```
String? Function([String?]) byField(
E entity,
String key, {
Function(List<ValidationException>)? overrideCallback,
}) {
if (key.contains('.')) {
final keys = key.split('.');

final firstKey = keys.removeAt(0);
final builder = _getBuilderByKey(firstKey);
if (builder == null) {
if (overrideCallback != null) {
overrideCallback.call([]);
return ([_]) => null;
}

return ([_]) => null;
}

Expand All @@ -61,15 +78,25 @@ abstract class LucidValidator<E> {
final builder = _getBuilderByKey(key);

if (builder == null) {
if (overrideCallback != null) {
overrideCallback.call([]);
return ([_]) => null;
}

return ([_]) => null;
}

return ([_]) {
final errors = builder.executeRules(entity);
if (errors.isNotEmpty) {
callback?.call(errors);
if (overrideCallback != null) {
overrideCallback.call(errors);
}
return errors.first.message;
}
if (overrideCallback != null) {
overrideCallback.call([]);
}
return null;
};
}
Expand Down
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: 1.2.2
version: 1.2.3

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
4 changes: 2 additions & 2 deletions test/lucid_validation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ void main() {
expect(codes[1], 'validEmail');
});

test('byField with callback', () {
test('byField with override callback', () {
var user = UserModel();

final validator = UserValidator();
List<String> codes = [];

validator.byField(user, 'email', (exceptions) {
validator.byField(user, 'email', overrideCallback: (exceptions) {
codes = exceptions.map((exception) => exception.code).toList();
})();

Expand Down

0 comments on commit 8075437

Please sign in to comment.