File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
lib/new_api_prototype/core_validators
test/new_api_testing/core_validators/conditional_validators Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -7,3 +7,12 @@ Validator<T> validateIf<T extends Object?>(
77 bool Function (T value) condition, Validator <T > v) {
88 return (T value) => condition (value) ? v (value) : null ;
99}
10+
11+ /// This function returns a validator that conditionally skips validation for
12+ /// the user input. If `condition` applied to the user input evaluates to true,
13+ /// the validation step is skipped and null is returned. Otherwise, it is
14+ /// validated by 'v'.
15+ Validator <T > skipIf <T extends Object ?>(
16+ bool Function (T value) condition, Validator <T > v) {
17+ return (T value) => condition (value) ? null : v (value);
18+ }
Original file line number Diff line number Diff line change 1+ import 'package:flutter_test/flutter_test.dart' ;
2+ import 'package:form_builder_validators/form_builder_validators.dart' ;
3+ import 'package:form_builder_validators/new_api_prototype/core_validators/conditional_validators.dart' ;
4+
5+ const String errorGt = 'error gt' ;
6+ Validator <num > gt (num target) {
7+ return (num v) {
8+ return v > target ? null : errorGt;
9+ };
10+ }
11+
12+ void main () {
13+ group ('Validator: skipIf' , () {
14+ test ('Should check if a number is greater than 10 if it is an even int' ,
15+ () {
16+ // Only even integers will be validated: ... 0, 2, 4 ...
17+ final Validator <num > v =
18+ skipIf ((num v) => v is ! int || v % 2 != 0 , gt (10 ));
19+
20+ expect (v (2.3 ), isNull);
21+
22+ expect (v (8 ), errorGt);
23+ expect (v (10 ), errorGt);
24+ expect (v (12 ), isNull); // This is null because it is valid
25+ expect (v (13 ), isNull); // This is null because it was not validated.
26+ });
27+ });
28+ }
You can’t perform that action at this time.
0 commit comments