Skip to content

Commit 0f63d93

Browse files
Implement min length validator
1 parent 71f6254 commit 0f63d93

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

lib/new_api_prototype/collection_validators.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import '../localization/l10n.dart';
22
import 'constants.dart';
33

4+
/// Returns a [Validator] function that checks if its input has a length that is
5+
/// greater than or equal to `minLength`. If the input satisfies this condition,
6+
/// the validator returns `null`. Otherwise, it returns the default error message
7+
/// `FormBuilderLocalizations.current.minLengthErrorText(minLength)`,
8+
/// if [minLengthMsg] is not provided.
9+
///
10+
/// # Caveats
11+
/// - Objects that are not collections are considered as collections with
12+
/// length 1.
13+
///
14+
/// # Errors
15+
/// - Throws [AssertionError] if `minLength` is negative.
416
Validator<T> minLength<T extends Object>(int minLength,
517
{String Function(int)? minLengthMsg}) {
6-
return (value) {
18+
assert(minLength >= 0, 'The "minLength" parameter may not be negative');
19+
return (T value) {
720
// I think it makes more sense to say that scalar objects has length 1 instead of 0.
821
int valueLength = 1;
922

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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: minLength', () {
6+
group('Validators with default error message', () {
7+
test('Should validate the input when it is expected to have minLength 0',
8+
() {
9+
final Validator<List<int>> v = minLength(0);
10+
final Validator<Object> v2 = minLength(0);
11+
12+
expect(v(<int>[]), isNull);
13+
expect(v2(<int>[12]), isNull);
14+
expect(v2(12), isNull);
15+
});
16+
test('Should validate the input when it is expected to have minLength 1',
17+
() {
18+
final Validator<List<int>> v = minLength(1);
19+
final Validator<Object> v2 = minLength(1);
20+
21+
expect(
22+
v(<int>[]), FormBuilderLocalizations.current.minLengthErrorText(1));
23+
expect(v(<int>[0]), isNull);
24+
expect(v(<int>[1, 2]), isNull);
25+
26+
expect(v2(<String>[]),
27+
FormBuilderLocalizations.current.minLengthErrorText(1));
28+
expect(v2(<String>['0']), isNull);
29+
expect(v2(0), isNull);
30+
expect(v2(<Object>[1, '3']), isNull);
31+
});
32+
test('Should validate the input when it is expected to have minLength 4',
33+
() {
34+
final Validator<List<String>> v = minLength(4);
35+
final Validator<String> v2 = minLength(4);
36+
37+
expect(v(<String>[]),
38+
FormBuilderLocalizations.current.minLengthErrorText(4));
39+
expect(v(<String>['1']),
40+
FormBuilderLocalizations.current.minLengthErrorText(4));
41+
expect(v(<String>['1', '2']),
42+
FormBuilderLocalizations.current.minLengthErrorText(4));
43+
expect(v(<String>['1', '2', '3']),
44+
FormBuilderLocalizations.current.minLengthErrorText(4));
45+
expect(v(<String>['1', '2', '3', '4']), isNull);
46+
expect(v(<String>['1', '2', '3', '4', '5']), isNull);
47+
expect(v(<String>['1', '2', '3', '4', '5', '6']), isNull);
48+
49+
expect(v2(''), FormBuilderLocalizations.current.minLengthErrorText(4));
50+
expect(v2('1'), FormBuilderLocalizations.current.minLengthErrorText(4));
51+
expect(
52+
v2('12'), FormBuilderLocalizations.current.minLengthErrorText(4));
53+
expect(
54+
v2('123'), FormBuilderLocalizations.current.minLengthErrorText(4));
55+
expect(v2('1234'), isNull);
56+
expect(v2('12345'), isNull);
57+
expect(v2('123456'), isNull);
58+
});
59+
});
60+
61+
test('Should validate input returning custom message error when invalid',
62+
() {
63+
const String customMsg = 'custom msg';
64+
final Validator<Object> v = minLength(3, minLengthMsg: (_) => customMsg);
65+
66+
expect(v(89), equals(customMsg));
67+
expect(v(<Object>[1, '2', 3, 4]), isNull);
68+
});
69+
test('Should throw AssertionError when minLength is negative', () {
70+
expect(() => minLength(-2), throwsAssertionError);
71+
});
72+
});
73+
}

0 commit comments

Comments
 (0)