Skip to content

Commit ee7509a

Browse files
committed
feat(validators): maxLength and minLength can now validate Iterable values length
1 parent 40c4de9 commit ee7509a

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

packages/form_builder_validators/lib/src/form_builder_validators.dart

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,20 @@ class FormBuilderValidators {
111111

112112
/// [FormFieldValidator] that requires the length of the field's value to be
113113
/// greater than or equal to the provided minimum length.
114-
static FormFieldValidator<String> minLength(
114+
static FormFieldValidator<T> minLength<T>(
115115
BuildContext context,
116116
int minLength, {
117117
bool allowEmpty = false,
118118
String? errorText,
119119
}) {
120120
assert(minLength > 0);
121-
return (valueCandidate) {
122-
final valueLength = valueCandidate?.length ?? 0;
121+
return (T? valueCandidate) {
122+
assert(valueCandidate is String ||
123+
valueCandidate is Iterable ||
124+
valueCandidate == null);
125+
var valueLength = 0;
126+
if (valueCandidate is String) valueLength = valueCandidate.length;
127+
if (valueCandidate is Iterable) valueLength = valueCandidate.length;
123128
return valueLength < minLength && (!allowEmpty || valueLength > 0)
124129
? errorText ??
125130
FormBuilderLocalizations.of(context).minLengthErrorText(minLength)
@@ -129,17 +134,24 @@ class FormBuilderValidators {
129134

130135
/// [FormFieldValidator] that requires the length of the field's value to be
131136
/// less than or equal to the provided maximum length.
132-
static FormFieldValidator<String> maxLength(
137+
static FormFieldValidator<T> maxLength<T>(
133138
BuildContext context,
134139
int maxLength, {
135140
String? errorText,
136141
}) {
137142
assert(maxLength > 0);
138-
return (valueCandidate) => null != valueCandidate &&
139-
valueCandidate.length > maxLength
140-
? errorText ??
141-
FormBuilderLocalizations.of(context).maxLengthErrorText(maxLength)
142-
: null;
143+
return (T? valueCandidate) {
144+
assert(valueCandidate is String ||
145+
valueCandidate is Iterable ||
146+
valueCandidate == null);
147+
var valueLength = 0;
148+
if (valueCandidate is String) valueLength = valueCandidate.length;
149+
if (valueCandidate is Iterable) valueLength = valueCandidate.length;
150+
return null != valueCandidate && valueLength > maxLength
151+
? errorText ??
152+
FormBuilderLocalizations.of(context).maxLengthErrorText(maxLength)
153+
: null;
154+
};
143155
}
144156

145157
/// [FormFieldValidator] that requires the field's value to be a valid email address.

packages/form_builder_validators/test/form_builder_validators_test.dart

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ void main() {
104104
}));
105105

106106
testWidgets(
107-
'FormBuilderValidators.maxLength',
107+
'FormBuilderValidators.maxLength for String',
108108
(WidgetTester tester) => testValidations(tester, (context) {
109-
final validator = FormBuilderValidators.maxLength(context, 5);
109+
final validator =
110+
FormBuilderValidators.maxLength<String>(context, 5);
110111
// Pass
111112
expect(validator(null), isNull);
112113
expect(validator(''), isNull);
@@ -116,10 +117,12 @@ void main() {
116117
expect(validator('something long'), isNotNull);
117118
expect(validator('123456'), isNotNull);
118119
}));
120+
119121
testWidgets(
120-
'FormBuilderValidators.minLength',
122+
'FormBuilderValidators.minLength for String',
121123
(WidgetTester tester) => testValidations(tester, (context) {
122-
final validator = FormBuilderValidators.minLength(context, 5);
124+
final validator =
125+
FormBuilderValidators.minLength<String>(context, 5);
123126
// Pass
124127
expect(validator('12345'), isNull);
125128
expect(validator('123456'), isNull);
@@ -129,11 +132,45 @@ void main() {
129132
expect(validator(''), isNotNull);
130133
expect(validator('two'), isNotNull);
131134
// Advanced
132-
final validatorAllowEmpty =
133-
FormBuilderValidators.minLength(context, 5, allowEmpty: true);
135+
final validatorAllowEmpty = FormBuilderValidators.minLength<String>(
136+
context, 5,
137+
allowEmpty: true);
134138
expect(validatorAllowEmpty(null), isNull);
135139
expect(validatorAllowEmpty(''), isNull);
136140
}));
141+
testWidgets(
142+
'FormBuilderValidators.maxLength for Iterable',
143+
(WidgetTester tester) => testValidations(tester, (context) {
144+
final validator =
145+
FormBuilderValidators.maxLength<Iterable<String>>(context, 3);
146+
// Pass
147+
expect(validator(null), isNull);
148+
expect(validator([]), isNull);
149+
expect(validator(['one', 'two']), isNull);
150+
expect(validator(['one', 'two', 'three']), isNull);
151+
// Fail
152+
expect(validator(['one', 'two', 'three', 'four']), isNotNull);
153+
}));
154+
155+
testWidgets(
156+
'FormBuilderValidators.minLength for Iterable',
157+
(WidgetTester tester) => testValidations(tester, (context) {
158+
final validator =
159+
FormBuilderValidators.minLength<Iterable<String>>(context, 3);
160+
// Pass
161+
expect(validator(['one', 'two', 'three']), isNull);
162+
expect(validator(['one', 'two', 'three', 'four']), isNull);
163+
// Fail
164+
expect(validator(null), isNotNull);
165+
expect(validator([]), isNotNull);
166+
expect(validator(['one', 'two']), isNotNull);
167+
// Advanced
168+
final validatorAllowEmpty = FormBuilderValidators.minLength<Iterable<String>>(
169+
context, 3,
170+
allowEmpty: true);
171+
expect(validatorAllowEmpty(null), isNull);
172+
expect(validatorAllowEmpty([]), isNull);
173+
}));
137174

138175
testWidgets(
139176
'FormBuilderValidators.email',

0 commit comments

Comments
 (0)