Skip to content

Commit 256d2a4

Browse files
make 'or' validator immutable
1 parent 970321f commit 256d2a4

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

lib/src/form_builder_validators.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,6 @@ final class Validators {
18271827
/// - [ArgumentError] if `validators` is empty
18281828
/// {@endtemplate}
18291829
static Validator<T> or<T extends Object>(
1830-
// TODO(ArturAssisComp): refactor to make this input immutable.
18311830
List<Validator<T>> validators, {
18321831
String prefix = '',
18331832
String suffix = '',

lib/src/validators/core_validators/compose_validators.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ Validator<T> or<T extends Object>(
4646
throw ArgumentError.value(
4747
'[]', 'validators', 'The list of validators must not be empty');
4848
}
49+
final List<Validator<T>> immutableValidators =
50+
List<Validator<T>>.unmodifiable(validators);
4951
return (T value) {
5052
final List<String> errorMessageBuilder = <String>[];
51-
for (final Validator<T> validator in validators) {
53+
for (final Validator<T> validator in immutableValidators) {
5254
final String? errorMessage = validator(value);
5355
if (errorMessage == null) {
5456
return null;

test/src/validators/core_validators/composition_validators/or_validator_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,26 @@ void main() {
104104
test('Should throw AssertionError when the validators input is empty', () {
105105
expect(() => or(<Validator<Object?>>[]), throwsArgumentError);
106106
});
107+
108+
test('should be immutable even when input validators change', () {
109+
final List<String? Function(double)> validators =
110+
<String? Function(double)>[ltE(10), gt(17)];
111+
112+
final Validator<double> v = or(validators);
113+
expect(v(12), stringContainsInOrder(<String>[errorLtE, errorGt]));
114+
expect(v(10), isNull);
115+
expect(v(19.0), isNull);
116+
117+
validators.add(gt(11));
118+
expect(v(12), stringContainsInOrder(<String>[errorLtE, errorGt]));
119+
expect(v(10), isNull);
120+
expect(v(19.0), isNull);
121+
122+
validators.removeLast();
123+
validators.removeLast();
124+
expect(v(12), stringContainsInOrder(<String>[errorLtE, errorGt]));
125+
expect(v(10), isNull);
126+
expect(v(19.0), isNull);
127+
});
107128
});
108129
}

0 commit comments

Comments
 (0)