-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: aadded overrideCallback in byField
- Loading branch information
Showing
4 changed files
with
39 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters