Skip to content

Commit 28a7c43

Browse files
implement isAfter validator
1 parent 071f3f5 commit 28a7c43

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'constants.dart';
2+
3+
String tmpIsAfterErrorMsg(DateTime reference) =>
4+
'The date must be after $reference';
5+
6+
/// This function returns a validator that checks if the user [DateTime] input is
7+
/// after (or equal, if `inclusive` is true) to `reference`. If the checking results
8+
/// true, the validators returns `null`. Otherwise, it returns `isAfterMsg`, if
9+
/// provided, or `FormBuilderLocalizations.current.isAfterErrorText`.
10+
Validator<DateTime> isAfter(
11+
DateTime reference, {
12+
String Function(DateTime)? isAfterMsg,
13+
bool inclusive = false,
14+
}) {
15+
return (DateTime value) {
16+
return value.isAfter(reference) ||
17+
(inclusive ? value.isAtSameMomentAs(reference) : false)
18+
? null
19+
: isAfterMsg?.call(reference) ?? tmpIsAfterErrorMsg(reference);
20+
};
21+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:form_builder_validators/new_api_prototype/constants.dart';
3+
import 'package:form_builder_validators/new_api_prototype/datetime_validators.dart';
4+
5+
void main() {
6+
group('Validator: isAfter', () {
7+
test('Validation for the year 1994', () {
8+
final DateTime reference = DateTime(1994);
9+
final DateTime eq = reference.copyWith();
10+
final DateTime after10Years = DateTime(2004);
11+
final DateTime after1Ms = reference.add(const Duration(milliseconds: 1));
12+
final DateTime before1Year = DateTime(1993);
13+
final DateTime before1Sec =
14+
reference.subtract(const Duration(seconds: 1));
15+
final Validator<DateTime> v = isAfter(reference);
16+
final String errorMsg = tmpIsAfterErrorMsg(reference);
17+
18+
expect(
19+
v(eq),
20+
errorMsg,
21+
reason: 'Should return error against 1994',
22+
);
23+
expect(
24+
v(after10Years),
25+
isNull,
26+
reason: 'Should return null against 2004',
27+
);
28+
expect(
29+
v(after1Ms),
30+
isNull,
31+
reason: 'Should return null against 1994 + 1ms',
32+
);
33+
expect(
34+
v(before1Year),
35+
errorMsg,
36+
reason: 'Should return errorMsg against 1993',
37+
);
38+
expect(
39+
v(before1Sec),
40+
errorMsg,
41+
reason: 'Should return errorMsg against 1994 - 1s',
42+
);
43+
});
44+
test(
45+
'Inclusive validation for datetime 2089, month 3, day 23, h 3, min 46, s 12, 233 ms',
46+
() {
47+
final DateTime reference = DateTime(2089, 3, 23, 3, 46, 12, 233);
48+
final DateTime eq = reference.copyWith();
49+
final DateTime after10Years = reference.copyWith(year: 2099);
50+
final DateTime after1Ms = reference.add(const Duration(milliseconds: 1));
51+
final DateTime before1Year = reference.copyWith(year: 2088);
52+
final DateTime before1Sec =
53+
reference.subtract(const Duration(seconds: 1));
54+
final Validator<DateTime> v = isAfter(reference, inclusive: true);
55+
final String errorMsg = tmpIsAfterErrorMsg(reference);
56+
57+
expect(
58+
v(eq),
59+
isNull,
60+
reason: 'Should return null against the same datetime',
61+
);
62+
expect(
63+
v(after10Years),
64+
isNull,
65+
reason: 'Should return null against the reference shifted +10 years',
66+
);
67+
expect(
68+
v(after1Ms),
69+
isNull,
70+
reason: 'Should return null against the reference shifted +1 ms',
71+
);
72+
expect(
73+
v(before1Year),
74+
errorMsg,
75+
reason:
76+
'Should return errorMsg against a datetime 1 year before the reference',
77+
);
78+
expect(
79+
v(before1Sec),
80+
errorMsg,
81+
reason:
82+
'Should return errorMsg against a datetime 1 sec before the reference',
83+
);
84+
});
85+
86+
test('Should return a custom message after validating', () {
87+
const String errorMsg = 'error msg';
88+
final DateTime reference = DateTime(2);
89+
final Validator<DateTime> v =
90+
isAfter(reference, isAfterMsg: (_) => errorMsg);
91+
92+
expect(
93+
v(reference.copyWith()),
94+
errorMsg,
95+
reason:
96+
'Should return custom message when input is equal to the reference',
97+
);
98+
expect(
99+
v(reference.subtract(const Duration(microseconds: 1))),
100+
errorMsg,
101+
reason:
102+
'Should return custom message when input is before the reference',
103+
);
104+
expect(
105+
v(reference.add(const Duration(days: 1))),
106+
isNull,
107+
reason: 'Should return null when the input is after the reference',
108+
);
109+
});
110+
});
111+
}

0 commit comments

Comments
 (0)