Skip to content

Commit c4c829a

Browse files
implement isEqual
1 parent 16fac8a commit c4c829a

File tree

3 files changed

+117
-41
lines changed

3 files changed

+117
-41
lines changed
Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,6 @@ class _HomePageState extends State<_HomePage> {
7979
validator: isRequired(isInt(isEqual(referenceValue))),
8080
),
8181
const SizedBox(height: 5),
82-
TextFormField(
83-
decoration:
84-
const InputDecoration(labelText: 'isEqual (dynamic)'),
85-
autovalidateMode: AutovalidateMode.always,
86-
validator: isRequired(
87-
isInt(isEqual(null, dynValue: () => referenceValue))),
88-
),
89-
const SizedBox(height: 5),
9082
TextFormField(
9183
decoration:
9284
const InputDecoration(labelText: 'isEqual (old API)'),
@@ -105,15 +97,6 @@ class _HomePageState extends State<_HomePage> {
10597
validator: isRequired(isInt(isEqual(referenceValue))),
10698
),
10799
const SizedBox(height: 5),
108-
FormBuilderTextField(
109-
name: 'isEqual (dynamic)',
110-
autovalidateMode: AutovalidateMode.always,
111-
decoration:
112-
const InputDecoration(labelText: 'isEqual (dynamic)'),
113-
validator: isRequired(
114-
isInt(isEqual(null, dynValue: () => referenceValue))),
115-
),
116-
const SizedBox(height: 5),
117100
FormBuilderTextField(
118101
name: 'isEqual (old API)',
119102
autovalidateMode: AutovalidateMode.always,
@@ -138,14 +121,6 @@ class _HomePageState extends State<_HomePage> {
138121
isRequired(isInt(isEqual(referenceValue))),
139122
),
140123
const SizedBox(height: 5),
141-
FormBuilderTextField(
142-
name: 'isEqual (dynamic)',
143-
decoration: const InputDecoration(
144-
labelText: 'isEqual (dynamic)'),
145-
validator: isRequired(isInt(
146-
isEqual(null, dynValue: () => referenceValue))),
147-
),
148-
const SizedBox(height: 5),
149124
FormBuilderTextField(
150125
name: 'isEqual (old API)',
151126
decoration: const InputDecoration(

lib/new_api_prototype/core_validators/equality_validators.dart

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,23 @@ import '../../localization/l10n.dart';
22
import '../constants.dart';
33

44
/// This function returns a validator that checks if the user input is equal (using
5-
/// the == operator) to either `value` or `dynValue()`. If the condition is satisfied,
5+
/// the == operator) to either `value`. If the condition is satisfied,
66
/// the validator returns null, otherwise, it returns
77
/// `FormBuilderLocalizations.current.equalErrorText(userInput.toString())`, if
88
/// `equalMsg` is not provided.
99
///
10-
/// # Parameters
11-
/// Exactly one of the inputs `value` or `dynValue` must be provided. If the value
12-
/// to be compared with is static, provide `value`. Otherwise, provide `dynValue`.
13-
///
1410
/// # Error
1511
/// - Throws [AssertionError] if not exactly one of `value` or `dynValue` was
1612
/// provided.
1713
Validator<T> isEqual<T extends Object?>(
18-
T? value, {
19-
T Function()? dynValue,
20-
String Function(String)? equalMsg,
14+
T value, {
15+
String Function(String)? isEqualMsg,
2116
}) {
22-
assert(
23-
(value != null && dynValue == null) ||
24-
(value == null && dynValue != null),
25-
'Exactly one of "value" or "dynValue" must be provided');
2617
return (T input) {
27-
final T? currentValue = value ?? dynValue?.call();
28-
final String valueString = currentValue!.toString();
29-
return currentValue == input
18+
final String valueString = value.toString();
19+
return value == input
3020
? null
31-
: equalMsg?.call(valueString) ??
21+
: isEqualMsg?.call(valueString) ??
3222
FormBuilderLocalizations.current.equalErrorText(valueString);
3323
};
3424
}
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/form_builder_validators.dart';
3+
4+
class _CustomClass {}
5+
6+
void main() {
7+
final _CustomClass myObject = _CustomClass();
8+
9+
group('Validator: isEqual', () {
10+
final List<
11+
({
12+
String description,
13+
bool testFails,
14+
Object? referenceValue,
15+
Object? userInput
16+
})> testCases = <({
17+
String description,
18+
Object? referenceValue,
19+
Object? userInput,
20+
bool testFails
21+
})>[
22+
// Edge cases
23+
(
24+
description: 'Should match the null',
25+
referenceValue: null,
26+
userInput: null,
27+
testFails: false
28+
),
29+
(
30+
description: 'Should not match the null',
31+
referenceValue: null,
32+
userInput: 123,
33+
testFails: true
34+
),
35+
(
36+
description: 'Should match the empty string',
37+
referenceValue: '',
38+
userInput: '',
39+
testFails: false
40+
),
41+
(
42+
description: 'Should not match the empty string',
43+
referenceValue: '',
44+
userInput: ' ',
45+
testFails: true
46+
),
47+
// Domain cases
48+
(
49+
description: 'Should match the integer 123',
50+
referenceValue: 123,
51+
userInput: 123,
52+
testFails: false
53+
),
54+
(
55+
description: 'Should match the string "Hello, World!"',
56+
referenceValue: 'Hello, World!',
57+
userInput: 'Hello, World!',
58+
testFails: false
59+
),
60+
(
61+
description: 'Should not match the string "Hello, World!"',
62+
referenceValue: 'Hello, World!',
63+
userInput: 'Hello, World!\n',
64+
testFails: true
65+
),
66+
(
67+
description: 'Should match a custom class object',
68+
referenceValue: myObject,
69+
userInput: myObject,
70+
testFails: false
71+
),
72+
(
73+
description: 'Should not match a custom class object',
74+
referenceValue: myObject,
75+
userInput: _CustomClass(),
76+
testFails: true
77+
),
78+
];
79+
80+
for (final (
81+
description: String desc,
82+
referenceValue: Object? referenceValue,
83+
userInput: Object? userInput,
84+
testFails: bool testFails
85+
) in testCases) {
86+
test(desc, () {
87+
final Validator<Object?> v = isEqual(referenceValue);
88+
89+
expect(
90+
v(userInput),
91+
testFails
92+
? equals(FormBuilderLocalizations.current
93+
.equalErrorText(referenceValue.toString()))
94+
: isNull);
95+
});
96+
}
97+
98+
test('Should return custom error message', () {
99+
const String ref = 'hello';
100+
const String customErrorMessage = 'custom error';
101+
final Validator<Object> v =
102+
isEqual(ref, isEqualMsg: (_) => customErrorMessage);
103+
104+
// success
105+
expect(v(ref), isNull);
106+
107+
// failure
108+
expect(v(123), customErrorMessage);
109+
});
110+
});
111+
}

0 commit comments

Comments
 (0)