Skip to content

Commit 6a3ac04

Browse files
implement isString validator tests
1 parent c6df4e6 commit 6a3ac04

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

lib/new_api_prototype/core_validators/required_validators.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import '../constants.dart';
44
/// This function generates a validator that enforces a required field rule. If
55
/// the input is not provided (i.e., null or empty), the function returns an
66
/// error message indicating that the field is required. If the input is
7-
/// provided, the function applies an additional validator v (if supplied) to
7+
/// provided, the function applies an additional validator `next` (if supplied) to
88
/// further validate the input.
99
Validator<T?> isRequired<T extends Object>([
10-
Validator<T>? v,
10+
Validator<T>? next,
1111
String? isRequiredMsg,
1212
]) {
1313
String? finalValidator(T? value) {
@@ -17,7 +17,7 @@ Validator<T?> isRequired<T extends Object>([
1717
return isRequiredMsg ??
1818
FormBuilderLocalizations.current.requiredErrorText;
1919
}
20-
return v?.call(transformedValue!);
20+
return next?.call(transformedValue!);
2121
}
2222

2323
return finalValidator;
@@ -37,10 +37,10 @@ String errorIsOptionalTemporary(String vErrorMessage) {
3737
/// This function generates a validator that marks a field as optional. If the
3838
/// user input is not provided (i.e., it's null or empty), the validator will
3939
/// return null, indicating no validation error. If the input is provided, the
40-
/// function applies an additional validator v (if provided) to further validate
40+
/// function applies an additional validator `next` (if provided) to further validate
4141
/// the input.
4242
Validator<T?> isOptional<T extends Object>([
43-
Validator<T>? v,
43+
Validator<T>? next,
4444
String Function(String)? isOptionalMsg,
4545
]) {
4646
String? finalValidator(T? value) {
@@ -50,7 +50,7 @@ Validator<T?> isOptional<T extends Object>([
5050
// field not provided
5151
return null;
5252
}
53-
final String? vErrorMessage = v?.call(transformedValue!);
53+
final String? vErrorMessage = next?.call(transformedValue!);
5454
if (vErrorMessage == null) {
5555
return null;
5656
}

lib/new_api_prototype/core_validators/type_validators.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
import '../../localization/l10n.dart';
44
import '../constants.dart';
55

6-
Validator<T> isString<T extends Object>(Validator<String>? v,
7-
{String? isStringMsg}) {
6+
const tmpIsStringMsg = 'This field requires a valid string.';
7+
8+
/// This function returns a validator that checks if the user input is a `String`.
9+
/// If it is a `String`, then it returns null when `next` is not provided. Otherwise,
10+
/// if `next` is provided, it passes the transformed value as `String` to the `next`
11+
/// validator.
12+
Validator<T> isString<T extends Object>([
13+
Validator<String>? next,
14+
String? isStringMsg,
15+
]) {
816
String? finalValidator(T value) {
9-
final (isValid, typeTransformedValue) = _isStringValidateAndConvert(value);
17+
final (bool isValid, String? typeTransformedValue) =
18+
_isStringValidateAndConvert(value);
1019
if (!isValid) {
11-
return isStringMsg ?? 'This field requires a valid string.';
20+
return isStringMsg ?? tmpIsStringMsg;
1221
}
13-
return v?.call(typeTransformedValue!);
22+
return next?.call(typeTransformedValue!);
1423
}
1524

1625
return finalValidator;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:form_builder_validators/form_builder_validators.dart';
3+
4+
void main() {
5+
const String errorMsg = 'error msg';
6+
String? hasLengthGreaterThan3(String input) =>
7+
input.length > 3 ? null : errorMsg;
8+
9+
group('Validator: isString', () {
10+
test('Should only check if the input is a String', () {
11+
final Validator<Object> v = isString();
12+
13+
expect(v(123), equals(tmpIsStringMsg));
14+
expect(v('123'), isNull);
15+
expect(v('1'), isNull);
16+
expect(v(''), isNull);
17+
});
18+
test('Should check if the input is a String with length greater than 3',
19+
() {
20+
final Validator<Object> v = isString(hasLengthGreaterThan3);
21+
22+
expect(v(123), equals(tmpIsStringMsg));
23+
expect(v('1234'), isNull);
24+
expect(v('12'), errorMsg);
25+
expect(v(''), errorMsg);
26+
});
27+
test('Should check if the input is a String with using custom error', () {
28+
const String customError = 'custom error';
29+
final Validator<Object> v = isString(null, customError);
30+
31+
expect(v(123), equals(customError));
32+
expect(v('1234'), isNull);
33+
expect(v('12'), isNull);
34+
});
35+
});
36+
}

0 commit comments

Comments
 (0)