Skip to content

Commit 7557dd0

Browse files
implement isBefore validator
1 parent 28a7c43 commit 7557dd0

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

lib/new_api_prototype/datetime_validators.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ String tmpIsAfterErrorMsg(DateTime reference) =>
55

66
/// This function returns a validator that checks if the user [DateTime] input is
77
/// after (or equal, if `inclusive` is true) to `reference`. If the checking results
8-
/// true, the validators returns `null`. Otherwise, it returns `isAfterMsg`, if
8+
/// true, the validator returns `null`. Otherwise, it returns `isAfterMsg`, if
99
/// provided, or `FormBuilderLocalizations.current.isAfterErrorText`.
1010
Validator<DateTime> isAfter(
1111
DateTime reference, {
@@ -19,3 +19,24 @@ Validator<DateTime> isAfter(
1919
: isAfterMsg?.call(reference) ?? tmpIsAfterErrorMsg(reference);
2020
};
2121
}
22+
23+
String tmpIsBeforeErrorMsg(DateTime reference) {
24+
return 'The date must be before ${reference.toLocal()}';
25+
}
26+
27+
/// This function returns a validator that checks if the user [DateTime] input is
28+
/// before (or equal, if `inclusive` is true) to `reference`. If the checking results
29+
/// true, the validator returns `null`. Otherwise, it returns `isBeforeMsg`, if
30+
/// provided, or `FormBuilderLocalizations.current.isBeforeErrorText`.
31+
Validator<DateTime> isBefore(
32+
DateTime reference, {
33+
String Function(DateTime)? isBeforeMsg,
34+
bool inclusive = false,
35+
}) {
36+
return (DateTime value) {
37+
return value.isBefore(reference) ||
38+
(inclusive ? value.isAtSameMomentAs(reference) : false)
39+
? null
40+
: isBeforeMsg?.call(reference) ?? tmpIsBeforeErrorMsg(reference);
41+
};
42+
}
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: isBefore', () {
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 = isBefore(reference);
16+
final String errorMsg = tmpIsBeforeErrorMsg(reference);
17+
18+
expect(
19+
v(eq),
20+
errorMsg,
21+
reason: 'Should return error against 1994',
22+
);
23+
expect(
24+
v(after10Years),
25+
errorMsg,
26+
reason: 'Should return error against 2004',
27+
);
28+
expect(
29+
v(after1Ms),
30+
errorMsg,
31+
reason: 'Should return error against 1994 + 1ms',
32+
);
33+
expect(
34+
v(before1Year),
35+
isNull,
36+
reason: 'Should return null against 1993',
37+
);
38+
expect(
39+
v(before1Sec),
40+
isNull,
41+
reason: 'Should return null 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 = isBefore(reference, inclusive: true);
55+
final String errorMsg = tmpIsBeforeErrorMsg(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+
errorMsg,
65+
reason: 'Should return error against the reference shifted +10 years',
66+
);
67+
expect(
68+
v(after1Ms),
69+
errorMsg,
70+
reason: 'Should return error against the reference shifted +1 ms',
71+
);
72+
expect(
73+
v(before1Year),
74+
isNull,
75+
reason:
76+
'Should return null against a datetime 1 year before the reference',
77+
);
78+
expect(
79+
v(before1Sec),
80+
isNull,
81+
reason:
82+
'Should return null 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+
isBefore(reference, isBeforeMsg: (_) => 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+
isNull,
101+
reason: 'Should return null when input is before the reference',
102+
);
103+
expect(
104+
v(reference.add(const Duration(days: 1))),
105+
errorMsg,
106+
reason:
107+
'Should return custom message when the input is after the reference',
108+
);
109+
});
110+
});
111+
}

0 commit comments

Comments
 (0)