@@ -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