Skip to content

Commit 4205fa9

Browse files
implement isNum validator tests
1 parent 39e13cb commit 4205fa9

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

lib/new_api_prototype/core_validators/type_validators.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,19 @@ Validator<T> isInt<T extends Object>([Validator<int>? next, String? isIntMsg]) {
6363
return (false, null);
6464
}
6565

66-
Validator<T> isNum<T extends Object>(Validator<num>? v, {String? isNumMsg}) {
66+
/// This function returns a validator that checks if the user input is either a
67+
/// `num` or a `String` that is parsable to a `num`.
68+
/// If it checks positive, then it returns null when `next` is not provided. Otherwise,
69+
/// if `next` is provided, it passes the transformed value as `num` to the `next`
70+
/// validator.
71+
Validator<T> isNum<T extends Object>([Validator<num>? next, String? isNumMsg]) {
6772
String? finalValidator(T value) {
68-
final (isValid, typeTransformedValue) = _isNumValidateAndConvert(value);
73+
final (bool isValid, num? typeTransformedValue) =
74+
_isNumValidateAndConvert(value);
6975
if (!isValid) {
7076
return isNumMsg ?? FormBuilderLocalizations.current.numericErrorText;
7177
}
72-
return v?.call(typeTransformedValue!);
78+
return next?.call(typeTransformedValue!);
7379
}
7480

7581
return finalValidator;

test/new_api_testing/core_validators/type_validators_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void main() {
66
String? hasLengthGreaterThan3(String input) =>
77
input.length > 3 ? null : errorMsg;
88
String? isEven(int input) => input % 2 == 0 ? null : errorMsg;
9+
String? greaterThan9(num input) => input > 9 ? null : errorMsg;
910

1011
group('Validator: isString', () {
1112
test('Should only check if the input is a String', () {
@@ -50,6 +51,8 @@ void main() {
5051
expect(v('123'), isNull);
5152
expect(v('1'), isNull);
5253
expect(v(24), isNull);
54+
expect(v(1 + 1), isNull);
55+
expect(v(1 ~/ 1), isNull);
5356
expect(v(-24), isNull);
5457
expect(v('-0'), isNull);
5558
});
@@ -71,4 +74,45 @@ void main() {
7174
expect(v(23), isNull);
7275
});
7376
});
77+
78+
group('Validator: isNum', () {
79+
test('Should only check if the input is a num/parsable to num', () {
80+
final Validator<Object> v = isNum();
81+
82+
expect(v('not an num'),
83+
equals(FormBuilderLocalizations.current.numericErrorText));
84+
expect(
85+
v('1-3'), equals(FormBuilderLocalizations.current.numericErrorText));
86+
expect(
87+
v(true), equals(FormBuilderLocalizations.current.numericErrorText));
88+
expect(v('123.0'), isNull);
89+
expect(v('123'), isNull);
90+
expect(v('1'), isNull);
91+
expect(v('1e3'), isNull);
92+
expect(v(24 / 3), isNull);
93+
expect(v(24), isNull);
94+
expect(v(-24), isNull);
95+
expect(v('-0'), isNull);
96+
});
97+
test('Should check if the input is an numeric greater than 9', () {
98+
final Validator<Object> v = isNum(greaterThan9);
99+
100+
expect(v('not an int'),
101+
equals(FormBuilderLocalizations.current.numericErrorText));
102+
expect(v('1234'), isNull);
103+
expect(v(10), isNull);
104+
expect(v(9), equals(errorMsg));
105+
expect(v(8), equals(errorMsg));
106+
expect(v(-4), equals(errorMsg));
107+
expect(v('-1234'), equals(errorMsg));
108+
});
109+
test('Should check if the input is a num using custom error', () {
110+
const String customError = 'custom error';
111+
final Validator<Object> v = isNum(null, customError);
112+
113+
expect(v('not num'), equals(customError));
114+
expect(v('23'), isNull);
115+
expect(v(-23.34), isNull);
116+
});
117+
});
74118
}

0 commit comments

Comments
 (0)