Skip to content

Commit 71f6254

Browse files
Implement max length validator
1 parent c4dd41d commit 71f6254

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

lib/new_api_prototype/collection_validators.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,22 @@ Validator<T> minLength<T extends Object>(int minLength,
1818
};
1919
}
2020

21+
/// Returns a [Validator] function that checks if its input has a length that is
22+
/// less than or equal to `maxLength`. If the input satisfies this condition,
23+
/// the validator returns `null`. Otherwise, it returns the default error message
24+
/// `FormBuilderLocalizations.current.maxLengthErrorText(maxLength)`,
25+
/// if [maxLengthMsg] is not provided.
26+
///
27+
/// # Caveats
28+
/// - Objects that are not collections are considered as collections with
29+
/// length 1.
30+
///
31+
/// # Errors
32+
/// - Throws [AssertionError] if `maxLength` is negative.
2133
Validator<T> maxLength<T extends Object>(int maxLength,
2234
{String Function(int)? maxLengthMsg}) {
23-
return (value) {
35+
assert(maxLength >= 0, 'The "maxLength" parameter may not be negative');
36+
return (T value) {
2437
// I think it makes more sense to say that scalar objects has length 1 instead of 0.
2538
int valueLength = 1;
2639

test/new_api_testing/collection_validators/equal_length_validator_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ void main() {
5454

5555
test('Should validate input returning custom message error when invalid',
5656
() {
57-
final customMsg = 'custom msg';
58-
final v = equalLength(3, equalLengthMsg: (_) => customMsg);
57+
const String customMsg = 'custom msg';
58+
final Validator<Object> v =
59+
equalLength(3, equalLengthMsg: (_) => customMsg);
5960

6061
expect(v('hey'), isNull);
6162
expect(v([1, '2', 3, 4]), equals(customMsg));
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:form_builder_validators/form_builder_validators.dart';
3+
4+
void main() {
5+
group('Validator: maxLength', () {
6+
group('Validators with default error message', () {
7+
test('Should validate the input when it is expected to have maxLength 0',
8+
() {
9+
final Validator<List<int>> v = maxLength(0);
10+
final Validator<Object> v2 = maxLength(0);
11+
12+
expect(v(<int>[]), isNull);
13+
expect(v2(<int>[12]),
14+
FormBuilderLocalizations.current.maxLengthErrorText(0));
15+
expect(v2(12), FormBuilderLocalizations.current.maxLengthErrorText(0));
16+
});
17+
test('Should validate the input when it is expected to have maxLength 1',
18+
() {
19+
final Validator<List<int>> v = maxLength(1);
20+
final Validator<Object> v2 = maxLength(1);
21+
22+
expect(v(<int>[]), isNull);
23+
expect(v(<int>[0]), isNull);
24+
expect(v2(<String>['0']), isNull);
25+
expect(v2(0), isNull);
26+
expect(v(<int>[1, 2]),
27+
FormBuilderLocalizations.current.maxLengthErrorText(1));
28+
expect(v2(<Object>[1, '3']),
29+
FormBuilderLocalizations.current.maxLengthErrorText(1));
30+
});
31+
test('Should validate the input when it is expected to have maxLength 4',
32+
() {
33+
final Validator<List<String>> v = maxLength(4);
34+
final Validator<String> v2 = maxLength(4);
35+
36+
expect(v(<String>[]), isNull);
37+
expect(v(<String>['1']), isNull);
38+
expect(v(<String>['1', '2']), isNull);
39+
expect(v(<String>['1', '2', '3']), isNull);
40+
expect(v(<String>['1', '2', '3', '4']), isNull);
41+
42+
expect(v2(''), isNull);
43+
expect(v2('1'), isNull);
44+
expect(v2('12'), isNull);
45+
expect(v2('123'), isNull);
46+
expect(v2('1234'), isNull);
47+
});
48+
});
49+
50+
test('Should validate input returning custom message error when invalid',
51+
() {
52+
const String customMsg = 'custom msg';
53+
final Validator<Object> v = maxLength(3, maxLengthMsg: (_) => customMsg);
54+
55+
expect(v('hey'), isNull);
56+
expect(v(<Object>[1, '2', 3, 4]), equals(customMsg));
57+
});
58+
test('Should throw AssertionError when maxLength is negative', () {
59+
expect(() => maxLength(-2), throwsAssertionError);
60+
});
61+
});
62+
}

0 commit comments

Comments
 (0)