Skip to content

Commit 3698588

Browse files
implement is optional validator
1 parent 0a05445 commit 3698588

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

lib/new_api_prototype/core_validators/required_validators.dart

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

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.
4+
/// This function generates a validator that enforces a required field rule. If
5+
/// the input is not provided (i.e., null or empty), the function returns an
6+
/// error message indicating that the field is required. If the input is
7+
/// provided, the function applies an additional validator v (if supplied) to
8+
/// further validate the input.
109
Validator<T?> isRequired<T extends Object>([
1110
Validator<T>? v,
1211
String? isRequiredMsg,
@@ -24,10 +23,19 @@ Validator<T?> isRequired<T extends Object>([
2423
return finalValidator;
2524
}
2625

27-
Validator<T?> isOptional<T extends Object>(
28-
Validator<T>? v, {
29-
String? isOptionalMsg,
30-
}) {
26+
String errorIsOptionalTemporary(String vErrorMessage) {
27+
return 'The field is optional, otherwise, $vErrorMessage';
28+
}
29+
30+
/// This function generates a validator that marks a field as optional. If the
31+
/// user input is not provided (i.e., it's null or empty), the validator will
32+
/// return null, indicating no validation error. If the input is provided, the
33+
/// function applies an additional validator v (if provided) to further validate
34+
/// the input.
35+
Validator<T?> isOptional<T extends Object>([
36+
Validator<T>? v,
37+
String Function(String)? isOptionalMsg,
38+
]) {
3139
String? finalValidator(T? value) {
3240
final (bool isValid, T? transformedValue) =
3341
_isRequiredValidateAndConvert(value);
@@ -40,7 +48,8 @@ Validator<T?> isOptional<T extends Object>(
4048
return null;
4149
}
4250

43-
return 'The field is optional, otherwise, $vErrorMessage';
51+
return isOptionalMsg?.call(vErrorMessage) ??
52+
errorIsOptionalTemporary(vErrorMessage);
4453
}
4554

4655
return finalValidator;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 = errorIsOptionalTemporary(errorMultBy6);
11+
12+
group('Validator: isOptional', () {
13+
test('Should make the input optional', () {
14+
final Validator<Object?> v = isOptional();
15+
16+
expect(v(null), isNull);
17+
expect(v(''), isNull);
18+
expect(v(<String>[]), isNull);
19+
expect(v(<int, String>{}), isNull);
20+
expect(v(123), isNull);
21+
expect(v(' '), isNull);
22+
expect(v('hello'), isNull);
23+
});
24+
25+
test('Should make the input optional with composed validator `v`', () {
26+
final Validator<int?> v = isOptional(isMultipleBy6);
27+
28+
expect(v(null), isNull);
29+
expect(v(0), isNull);
30+
expect(v(1), equals(defaultError));
31+
expect(v(5), equals(defaultError));
32+
expect(v(6), isNull);
33+
});
34+
35+
test('Should return custom message for invalid input', () {
36+
const String customMsg = 'custom error message ';
37+
final Validator<Object?> v = isOptional(null, (_) => customMsg);
38+
final Validator<int?> v1 = isOptional(isMultipleBy6, (_) => customMsg);
39+
40+
expect(v(null), isNull);
41+
expect(v(''), isNull);
42+
expect(v1(null), isNull);
43+
expect(v(0), isNull);
44+
expect(v1(1), customMsg);
45+
expect(v1(6), isNull);
46+
});
47+
});
48+
}

0 commit comments

Comments
 (0)