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