Skip to content

Commit 39e13cb

Browse files
implement isInt validator tests
1 parent 6a3ac04 commit 39e13cb

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-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
@@ -32,13 +32,19 @@ Validator<T> isString<T extends Object>([
3232
return (false, null);
3333
}
3434

35-
Validator<T> isInt<T extends Object>(Validator<int>? v, {String? isIntMsg}) {
35+
/// This function returns a validator that checks if the user input is either an
36+
/// `int` or a `String` that is parsable to an `int`.
37+
/// If it checks positive, then it returns null when `next` is not provided. Otherwise,
38+
/// if `next` is provided, it passes the transformed value as `int` to the `next`
39+
/// validator.
40+
Validator<T> isInt<T extends Object>([Validator<int>? next, String? isIntMsg]) {
3641
String? finalValidator(T value) {
37-
final (isValid, typeTransformedValue) = _isIntValidateAndConvert(value);
42+
final (bool isValid, int? typeTransformedValue) =
43+
_isIntValidateAndConvert(value);
3844
if (!isValid) {
3945
return isIntMsg ?? FormBuilderLocalizations.current.integerErrorText;
4046
}
41-
return v?.call(typeTransformedValue!);
47+
return next?.call(typeTransformedValue!);
4248
}
4349

4450
return finalValidator;

test/new_api_testing/core_validators/type_validators_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ void main() {
55
const String errorMsg = 'error msg';
66
String? hasLengthGreaterThan3(String input) =>
77
input.length > 3 ? null : errorMsg;
8+
String? isEven(int input) => input % 2 == 0 ? null : errorMsg;
89

910
group('Validator: isString', () {
1011
test('Should only check if the input is a String', () {
@@ -33,4 +34,41 @@ void main() {
3334
expect(v('12'), isNull);
3435
});
3536
});
37+
38+
group('Validator: isInt', () {
39+
test('Should only check if the input is an int/parsable to int', () {
40+
final Validator<Object> v = isInt();
41+
42+
expect(v('not an int'),
43+
equals(FormBuilderLocalizations.current.integerErrorText));
44+
expect(
45+
v('1-3'), equals(FormBuilderLocalizations.current.integerErrorText));
46+
expect(v('123.0'),
47+
equals(FormBuilderLocalizations.current.integerErrorText));
48+
expect(
49+
v(123.0), equals(FormBuilderLocalizations.current.integerErrorText));
50+
expect(v('123'), isNull);
51+
expect(v('1'), isNull);
52+
expect(v(24), isNull);
53+
expect(v(-24), isNull);
54+
expect(v('-0'), isNull);
55+
});
56+
test('Should check if the input is an even integer', () {
57+
final Validator<Object> v = isInt(isEven);
58+
59+
expect(v('not an int'),
60+
equals(FormBuilderLocalizations.current.integerErrorText));
61+
expect(v('1234'), isNull);
62+
expect(v(-4), isNull);
63+
expect(v('1233'), equals(errorMsg));
64+
});
65+
test('Should check if the input is an int using custom error', () {
66+
const String customError = 'custom error';
67+
final Validator<Object> v = isInt(null, customError);
68+
69+
expect(v('not int'), equals(customError));
70+
expect(v('23'), isNull);
71+
expect(v(23), isNull);
72+
});
73+
});
3674
}

0 commit comments

Comments
 (0)