Skip to content

Commit

Permalink
Merge pull request #7 from anotherpit/feature/should-pass
Browse files Browse the repository at this point in the history
Added shouldPass validation
  • Loading branch information
ganeshrvel committed Apr 18, 2021
2 parents 18f8a5f + 1f1dfa8 commit 1052077
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 220 deletions.
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -163,6 +163,8 @@ String shouldMatch;
String shouldNotMatch;
bool shouldPass(String value);
List<String> inList;
List<String> notInList;
Expand Down Expand Up @@ -735,6 +737,29 @@ void main() {
```

###### shouldPass: `bool Function(String value)`
- Checks if the input value passes the given function check
- It will throw an error if the function returns false

```dart
void main() {
const textFieldValue = 'abc';
final rule = Rule(
textFieldValue,
name: 'Text field',
shouldPass: (value) => value.contains('a') && value.contains('b')
);
if (rule.hasError) {
// some action on error
} else {
// Some action on success
}
}
```



###### inList: `List<String>`
Expand Down Expand Up @@ -813,6 +838,7 @@ void main() {
'notEqualToInList': '{name} should not be equal to any of these values $notEqualToInList'
'shouldMatch': '{name} should be same as $shouldMatch'
'shouldNotMatch': '{name} should not same as $shouldNotMatch'
'shouldPass': '{name} is invalid'
'inList': '{name} should be any of these values $inList'
'notInList': '{name} should not be any of these values $notInList'
```
Expand Down Expand Up @@ -1347,6 +1373,7 @@ void main() {
print(rule.value);
// output: abc
}
```

###### upperCase: `bool`
Expand All @@ -1371,6 +1398,7 @@ void main() {
print(rule.value);
// output: xyz
}
```

**Extension**
Expand Down
6 changes: 6 additions & 0 deletions lib/src/abstract_rule.dart
@@ -0,0 +1,6 @@
abstract class AbstractRule {
///
/// outputs true if there is a validation error else false
///
bool get hasError;
}
15 changes: 7 additions & 8 deletions lib/src/combined_rule.dart
@@ -1,3 +1,4 @@
import 'package:rules/src/abstract_rule.dart';
import 'package:rules/src/group_rule.dart';
import 'package:rules/src/helpers/functs.dart';
import 'package:rules/src/models/rule_model.dart';
Expand All @@ -8,7 +9,7 @@ import 'package:rules/src/rule.dart';
/// Refer https://github.com/ganeshrvel/pub-rules/blob/master/README.md#1-combinedrule for usage details
///
///
class CombinedRule {
class CombinedRule implements AbstractRule {
///
/// Rules list for validation
///
Expand All @@ -19,7 +20,7 @@ class CombinedRule {
///
final List<GroupRule?>? groupRules;

List<String?> _errorList = <String>[];
List<String> _errorList = <String>[];

CombinedRule({
this.rules,
Expand All @@ -33,11 +34,9 @@ class CombinedRule {
///
/// outputs the list of error texts
///
List<String?> get errorList => _ruleModel.errorList;
List<String> get errorList => _ruleModel.errorList;

///
/// outputs true if there is a validation error else false
///
@override
bool get hasError => isNotNullOrEmpty(errorList);

// starting point
Expand All @@ -53,7 +52,7 @@ class CombinedRule {

if (isNotNullOrEmpty(_error)) {
// spread the errors into [_errorList]
_errorList = [..._errorList, _error];
_errorList = [..._errorList, _error!];
}
}
}
Expand All @@ -65,7 +64,7 @@ class CombinedRule {

if (isNotNullOrEmpty(_error)) {
// spread the errors into [_errorList]
_errorList = [..._errorList, _error];
_errorList = [..._errorList, _error!];
}
}
}
Expand Down
29 changes: 10 additions & 19 deletions lib/src/group_rule.dart
@@ -1,3 +1,4 @@
import 'package:rules/src/abstract_rule.dart';
import 'package:rules/src/helpers/functs.dart';
import 'package:rules/src/helpers/group_rule_functs.dart';
import 'package:rules/src/helpers/strings.dart';
Expand All @@ -9,7 +10,7 @@ import 'package:rules/src/rule.dart';
/// Refer https://github.com/ganeshrvel/pub-rules/blob/master/README.md#2-grouprule for usage details
///
///
class GroupRule {
class GroupRule implements AbstractRule {
// Rules list for validation
final List<Rule?> rulesList;

Expand All @@ -28,11 +29,11 @@ class GroupRule {

// if the validator fails then the corresponding [_errorTextsDict] key is added to this array.
// which will be later used for parsing and outputing error text
final _errorItemList = <String>[];
final List<String> _errorItemList = <String>[];

// it holds the error texts; Note: maximum one error text, for now, is held here
// this can change in the future
final _errorList = <String?>[];
final List<String> _errorList = <String>[];

// default error text dictionary
Map<String, String> get _errorTextsDict => {
Expand Down Expand Up @@ -90,9 +91,7 @@ class GroupRule {
///
String? get error => _ruleModel.error;

///
/// outputs true if there is a validation error else false
///
@override
bool get hasError => isNotNullOrEmpty(error);

// starting point
Expand All @@ -115,26 +114,18 @@ class GroupRule {
final error = rule?.error;

if (isNotNullOrEmpty(error)) {
_errorList.add(error);
_errorList.add(error!);

break;
}
}
}

// the validation happens here
void _beginValidation() {
if (requiredAll == true && _isRequiredCheckFailed()) {
return;
}

if (requiredAtleast != null && _isRequiredAtleastCheckFailed()) {
return;
}

if (maxAllowed != null && _isMaxAllowedCheckFailed()) {
return;
}
bool _beginValidation() {
return (requiredAll == true && _isRequiredCheckFailed()) ||
(requiredAtleast != null && _isRequiredAtleastCheckFailed()) ||
(maxAllowed != null && _isMaxAllowedCheckFailed());
}

bool _isRequiredCheckFailed() {
Expand Down
53 changes: 5 additions & 48 deletions lib/src/helpers/array.dart
Expand Up @@ -5,59 +5,16 @@ bool inArray(List array, dynamic x) {
return array.contains(x);
}

// check if an index is present in an array
bool isArrayIndexExists(List<dynamic> array, int x) {
return array.isNotEmpty && x >= array.length - 1;
}

// return true if any value is found null
bool isNullExists(
List<dynamic> valueList, {
List<dynamic> list, {
bool isEmpty = false,
}) {
for (final val in valueList) {
if (isEmpty) {
if (isNullOrEmpty(val)) {
return true;
}
} else if (val == null) {
return true;
}
}

return false;
}
}) =>
list.indexWhere((v) => isEmpty ? isNullOrEmpty(v) : (v == null)) != -1;

// return true if any value is found not null
bool isNotNullExists(
List<dynamic> valueList, {
bool isEmpty = false,
}) {
for (final val in valueList) {
if (isEmpty) {
if (isNotNullOrEmpty(val)) {
return true;
}
} else if (val != null) {
return true;
}
}

return false;
}

// return all the null values in the array
List<String> getNullValues(Map<String, dynamic> valueList) {
final _returnList = <String>[];

for (final value in valueList.entries) {
if (value.value == null) {
_returnList.add(value.key);
}
}

return _returnList;
}
bool isNotNullExists(List<dynamic> list) =>
list.indexWhere((v) => v != null) != -1;

// parse [string] values in the array to [double]
List<double?> getParsedDoubleArray(List<String> valueList) {
Expand Down
9 changes: 0 additions & 9 deletions lib/src/helpers/date.dart

This file was deleted.

5 changes: 0 additions & 5 deletions lib/src/helpers/strings.dart
Expand Up @@ -90,11 +90,6 @@ bool isStringMaxLength(String input, int length) {
return input.length <= length;
}

// capitalize a string
String capitalize(String input) {
return input[0].toUpperCase() + input.substring(1);
}

// add plurality to a string
String plural(
String text, {
Expand Down
10 changes: 0 additions & 10 deletions lib/src/helpers/types.dart

This file was deleted.

6 changes: 2 additions & 4 deletions lib/src/models/rule_model.dart
@@ -1,17 +1,15 @@
import 'package:rules/src/helpers/array.dart';
import 'package:rules/src/helpers/functs.dart';

// rules model
class RuleModel {
String? error;
List<String?> errorList;
List<String> errorList;

RuleModel({
required this.errorList,
}) {
if (isNotNullOrEmpty(errorList) && isArrayIndexExists(errorList, 0)) {
if (isNotNullOrEmpty(errorList)) {
error = errorList[0];

return;
}

Expand Down

0 comments on commit 1052077

Please sign in to comment.