Skip to content

Commit 0a05445

Browse files
implement is required validator
1 parent 408a9d4 commit 0a05445

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

lib/new_api_prototype/core_validators/required_validators.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import '../../localization/l10n.dart';
22
import '../constants.dart';
33

4-
Validator<T?> isRequired<T extends Object>(
5-
Validator<T>? v, {
4+
/// This function returns a transformer validator that checks if the user input
5+
/// is neither null nor empty, in the case it is a collection, a string or a map.
6+
/// If the condition was not satisfied, it returns the `isRequiredMsg` error message,
7+
/// if provided, or ´FormBuilderLocalizations.current.requiredErrorText´ otherwise.
8+
/// If the condition is satisfied, it returns the validation of the input with
9+
/// `v` or null if `v` was not provided.
10+
Validator<T?> isRequired<T extends Object>([
11+
Validator<T>? v,
612
String? isRequiredMsg,
7-
}) {
13+
]) {
814
String? finalValidator(T? value) {
9-
final (isValid, transformedValue) = _isRequiredValidateAndConvert(value);
15+
final (bool isValid, T? transformedValue) =
16+
_isRequiredValidateAndConvert(value);
1017
if (!isValid) {
1118
return isRequiredMsg ??
1219
FormBuilderLocalizations.current.requiredErrorText;
@@ -22,12 +29,13 @@ Validator<T?> isOptional<T extends Object>(
2229
String? isOptionalMsg,
2330
}) {
2431
String? finalValidator(T? value) {
25-
final (isValid, transformedValue) = _isRequiredValidateAndConvert(value);
32+
final (bool isValid, T? transformedValue) =
33+
_isRequiredValidateAndConvert(value);
2634
if (!isValid) {
2735
// field not provided
2836
return null;
2937
}
30-
final vErrorMessage = v?.call(transformedValue!);
38+
final String? vErrorMessage = v?.call(transformedValue!);
3139
if (vErrorMessage == null) {
3240
return null;
3341
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:form_builder_validators/form_builder_validators.dart';
3+
4+
const String errorMultBy6 = 'error';
5+
String? isMultipleBy6(int value) {
6+
return value % 6 == 0 ? null : errorMultBy6;
7+
}
8+
9+
void main() {
10+
final String defaultError =
11+
FormBuilderLocalizations.current.requiredErrorText;
12+
13+
group('Validator: isRequired', () {
14+
test('Should check if the input value is not null/empty', () {
15+
final Validator<Object?> v = isRequired();
16+
17+
expect(v(null), defaultError);
18+
expect(v(''), defaultError);
19+
expect(v(<String>[]), defaultError);
20+
expect(v(<int, String>{}), defaultError);
21+
expect(v(123), isNull);
22+
expect(v(' '), defaultError);
23+
expect(v('hello'), isNull);
24+
});
25+
26+
test(
27+
'Should check if the input value is not null/empty with composed validator `v`',
28+
() {
29+
final Validator<int?> v = isRequired(isMultipleBy6);
30+
31+
expect(v(null), equals(defaultError));
32+
expect(v(0), isNull);
33+
expect(v(1), equals(errorMultBy6));
34+
expect(v(5), equals(errorMultBy6));
35+
expect(v(6), isNull);
36+
});
37+
38+
test('Should return custom message for null input', () {
39+
const String customMsg = 'custom error message ';
40+
final Validator<Object?> v = isRequired(null, customMsg);
41+
final Validator<int?> v1 = isRequired(isMultipleBy6, customMsg);
42+
43+
expect(v(null), equals(customMsg));
44+
expect(v(''), equals(customMsg));
45+
expect(v1(null), equals(customMsg));
46+
expect(v(0), isNull);
47+
expect(v1(1), equals(errorMultBy6));
48+
expect(v1(6), isNull);
49+
});
50+
});
51+
}

0 commit comments

Comments
 (0)