Skip to content

Commit 970321f

Browse files
make 'and' validator immutable
1 parent ef62499 commit 970321f

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

lib/src/form_builder_validators.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,6 @@ final class Validators {
17841784
/// - [ArgumentError] if `validators` is empty
17851785
/// {@endtemplate}
17861786
static Validator<T> and<T extends Object>(
1787-
// TODO(ArturAssisComp): refactor to make this input immutable.
17881787
List<Validator<T>> validators, {
17891788
String prefix = '',
17901789
String suffix = '',
@@ -4159,14 +4158,11 @@ final class Validators {
41594158
/// ```
41604159
/// {@endtemplate}
41614160
static Validator<String> url({
4162-
// TODO(ArturAssisComp): refactor to make this input immutable.
41634161
List<String> protocols = val.kDefaultUrlValidationProtocols,
41644162
bool requireTld = true,
41654163
bool requireProtocol = false,
41664164
bool allowUnderscore = false,
4167-
// TODO(ArturAssisComp): refactor to make this input immutable.
41684165
List<String> hostAllowList = const <String>[],
4169-
// TODO(ArturAssisComp): refactor to make this input immutable.
41704166
List<String> hostBlockList = const <String>[],
41714167
RegExp? regex,
41724168
String Function(String input)? urlMsg,

lib/src/validators/core_validators/compose_validators.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ Validator<T> and<T extends Object>(
1414
throw ArgumentError.value(
1515
'[]', 'validators', 'The list of validators must not be empty');
1616
}
17+
final List<Validator<T>> immutableValidators =
18+
List<Validator<T>>.unmodifiable(validators);
1719
return (T value) {
1820
final List<String> errorMessageBuilder = <String>[];
19-
for (final Validator<T> validator in validators) {
21+
for (final Validator<T> validator in immutableValidators) {
2022
final String? errorMessage = validator(value);
2123
if (errorMessage != null) {
2224
if (printErrorAsSoonAsPossible) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,29 @@ void main() {
135135
test('Should throw AssertionError when the validators input is empty', () {
136136
expect(() => and(<Validator<Object?>>[]), throwsArgumentError);
137137
});
138+
139+
test('should be immutable even when input validators change', () {
140+
final List<String? Function(double)> validators =
141+
<String? Function(double)>[gt(12), ltE(15)];
142+
143+
final Validator<double> v = and(validators);
144+
expect(v(12), errorGt);
145+
expect(v(13), isNull);
146+
expect(v(15), isNull);
147+
expect(v(16.5), errorLtE);
148+
149+
validators.add(gt(13));
150+
expect(v(12), errorGt);
151+
expect(v(13), isNull);
152+
expect(v(15), isNull);
153+
expect(v(16.5), errorLtE);
154+
155+
validators.removeLast();
156+
validators.removeLast();
157+
expect(v(12), errorGt);
158+
expect(v(13), isNull);
159+
expect(v(15), isNull);
160+
expect(v(16.5), errorLtE);
161+
});
138162
});
139163
}

0 commit comments

Comments
 (0)