Skip to content

Commit 6c4df09

Browse files
committed
Fixes
1 parent d4edb6f commit 6c4df09

File tree

6 files changed

+577
-51
lines changed

6 files changed

+577
-51
lines changed

lib/src/file/file_name_validator.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ import '../../localization/l10n.dart';
22
import '../base_validator.dart';
33

44
class FileNameValidator extends BaseValidator<String> {
5-
const FileNameValidator({
5+
FileNameValidator({
6+
/// {@macro filename_template}
7+
RegExp? regex,
8+
69
/// {@macro base_validator_error_text}
710
super.errorText,
811

912
/// {@macro base_validator_null_check}
1013
super.checkNullOrEmpty,
11-
});
14+
}) : regex = regex ?? _fileName;
15+
16+
final RegExp regex;
17+
18+
/// {@template filename_template}
19+
/// This regex matches any character that is not a valid file name character.
20+
/// - It includes special characters, digits, and lowercase letters.
21+
/// - It can be used to find invalid file name characters.
22+
/// Examples: a, 1, @
23+
/// {@endtemplate}
24+
static final RegExp _fileName = RegExp(r'^[a-zA-Z0-9_\-\.]+$');
1225

1326
@override
1427
String get translatedErrorText =>
@@ -20,6 +33,6 @@ class FileNameValidator extends BaseValidator<String> {
2033
}
2134

2235
bool isFileName(String valueCandidate) {
23-
return RegExp(r'^[a-zA-Z0-9_\-\.]+$').hasMatch(valueCandidate);
36+
return regex.hasMatch(valueCandidate);
2437
}
2538
}

lib/src/form_builder_validators.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,10 +786,12 @@ class FormBuilderValidators {
786786
/// - [errorText] The error message when the file name is invalid.
787787
/// - [checkNullOrEmpty] Whether to check for null or empty values.
788788
static FormFieldValidator<String> fileName({
789+
RegExp? regex,
789790
String? errorText,
790791
bool checkNullOrEmpty = true,
791792
}) =>
792793
FileNameValidator(
794+
regex: regex,
793795
errorText: errorText,
794796
checkNullOrEmpty: checkNullOrEmpty,
795797
).validate;

test/src/collection/min_length_validator_test.dart

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ void main() {
5151
// Assert
5252
expect(result, isNotNull);
5353
expect(
54-
result,
55-
equals(
56-
FormBuilderLocalizations.current.minLengthErrorText(minLength)));
54+
result,
55+
equals(
56+
FormBuilderLocalizations.current.minLengthErrorText(minLength),
57+
),
58+
);
5759
});
5860

5961
test(
@@ -100,9 +102,11 @@ void main() {
100102
// Assert
101103
expect(result, isNotNull);
102104
expect(
103-
result,
104-
equals(
105-
FormBuilderLocalizations.current.minLengthErrorText(minLength)));
105+
result,
106+
equals(
107+
FormBuilderLocalizations.current.minLengthErrorText(minLength),
108+
),
109+
);
106110
});
107111

108112
test(
@@ -136,9 +140,11 @@ void main() {
136140
// Assert
137141
expect(result, isNotNull);
138142
expect(
139-
result,
140-
equals(
141-
FormBuilderLocalizations.current.minLengthErrorText(minLength)));
143+
result,
144+
equals(
145+
FormBuilderLocalizations.current.minLengthErrorText(minLength),
146+
),
147+
);
142148
});
143149
});
144150

@@ -148,7 +154,7 @@ void main() {
148154
const int minLength = 3;
149155
const MinLengthValidator<List<String>> validator =
150156
MinLengthValidator<List<String>>(minLength);
151-
const List<String> value = ['a', 'b', 'c'];
157+
const List<String> value = <String>['a', 'b', 'c'];
152158

153159
// Act
154160
final String? result = validator.validate(value);
@@ -163,7 +169,7 @@ void main() {
163169
const int minLength = 3;
164170
const MinLengthValidator<List<String>> validator =
165171
MinLengthValidator<List<String>>(minLength);
166-
const List<String> value = ['a', 'b', 'c', 'd'];
172+
const List<String> value = <String>['a', 'b', 'c', 'd'];
167173

168174
// Act
169175
final String? result = validator.validate(value);
@@ -179,17 +185,19 @@ void main() {
179185
const int minLength = 3;
180186
const MinLengthValidator<List<String>> validator =
181187
MinLengthValidator<List<String>>(minLength);
182-
const List<String> value = ['a', 'b'];
188+
const List<String> value = <String>['a', 'b'];
183189

184190
// Act
185191
final String? result = validator.validate(value);
186192

187193
// Assert
188194
expect(result, isNotNull);
189195
expect(
190-
result,
191-
equals(
192-
FormBuilderLocalizations.current.minLengthErrorText(minLength)));
196+
result,
197+
equals(
198+
FormBuilderLocalizations.current.minLengthErrorText(minLength),
199+
),
200+
);
193201
});
194202

195203
test(
@@ -198,9 +206,11 @@ void main() {
198206
// Arrange
199207
const int minLength = 3;
200208
final MinLengthValidator<List<String>> validator =
201-
MinLengthValidator<List<String>>(minLength,
202-
errorText: customErrorMessage);
203-
const List<String> value = ['a', 'b'];
209+
MinLengthValidator<List<String>>(
210+
minLength,
211+
errorText: customErrorMessage,
212+
);
213+
const List<String> value = <String>['a', 'b'];
204214

205215
// Act
206216
final String? result = validator.validate(value);
@@ -237,9 +247,11 @@ void main() {
237247
// Assert
238248
expect(result, isNotNull);
239249
expect(
240-
result,
241-
equals(
242-
FormBuilderLocalizations.current.minLengthErrorText(minLength)));
250+
result,
251+
equals(
252+
FormBuilderLocalizations.current.minLengthErrorText(minLength),
253+
),
254+
);
243255
});
244256

245257
test('should return null when the list is empty and null check is disabled',
@@ -248,7 +260,7 @@ void main() {
248260
const int minLength = 3;
249261
const MinLengthValidator<List<String>> validator =
250262
MinLengthValidator<List<String>>(minLength, checkNullOrEmpty: false);
251-
const List<String> value = [];
263+
const List<String> value = <String>[];
252264

253265
// Act
254266
final String? result = validator.validate(value);
@@ -262,17 +274,19 @@ void main() {
262274
const int minLength = 3;
263275
const MinLengthValidator<List<String>> validator =
264276
MinLengthValidator<List<String>>(minLength);
265-
const List<String> value = [];
277+
const List<String> value = <String>[];
266278

267279
// Act
268280
final String? result = validator.validate(value);
269281

270282
// Assert
271283
expect(result, isNotNull);
272284
expect(
273-
result,
274-
equals(
275-
FormBuilderLocalizations.current.minLengthErrorText(minLength)));
285+
result,
286+
equals(
287+
FormBuilderLocalizations.current.minLengthErrorText(minLength),
288+
),
289+
);
276290
});
277291
});
278292
}

test/src/file/file_extension_validator_test.dart

Lines changed: 162 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import 'package:form_builder_validators/form_builder_validators.dart';
55
void main() {
66
final Faker faker = Faker.instance;
77
final String customErrorMessage = faker.lorem.sentence();
8-
group('File extension -', () {
9-
test('should return null when the value is not null', () {
8+
9+
group('FileExtensionValidator -', () {
10+
test('should return null when the file extension is in the allowed list',
11+
() {
1012
// Arrange
11-
const FileExtensionValidator validator = FileExtensionValidator();
12-
const String value = 'abc';
13+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
14+
const FileExtensionValidator validator =
15+
FileExtensionValidator(allowedExtensions);
16+
const String value = 'image.jpg';
1317

1418
// Act
1519
final String? result = validator.validate(value);
@@ -18,17 +22,167 @@ void main() {
1822
expect(result, isNull);
1923
});
2024

21-
test('should return the error message when the value is null', () {
25+
test(
26+
'should return null when the file extension is in the allowed list with mixed case',
27+
() {
2228
// Arrange
23-
final FileExtensionValidator validator =
24-
FileExtensionValidator(errorText: customErrorMessage);
25-
const String? value = null;
29+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
30+
const FileExtensionValidator validator =
31+
FileExtensionValidator(allowedExtensions);
32+
const String value = 'image.JPG';
33+
34+
// Act
35+
final String? result = validator.validate(value);
36+
37+
// Assert
38+
expect(result, isNull);
39+
});
40+
41+
test(
42+
'should return the default error message when the file extension is not in the allowed list',
43+
() {
44+
// Arrange
45+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
46+
const FileExtensionValidator validator =
47+
FileExtensionValidator(allowedExtensions);
48+
const String value = 'document.pdf';
49+
50+
// Act
51+
final String? result = validator.validate(value);
52+
53+
// Assert
54+
expect(result, isNotNull);
55+
expect(
56+
result,
57+
equals(
58+
FormBuilderLocalizations.current
59+
.fileExtensionErrorText(allowedExtensions.join(', ')),
60+
),
61+
);
62+
});
63+
64+
test(
65+
'should return the custom error message when the file extension is not in the allowed list',
66+
() {
67+
// Arrange
68+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
69+
final FileExtensionValidator validator = FileExtensionValidator(
70+
allowedExtensions,
71+
errorText: customErrorMessage,
72+
);
73+
const String value = 'document.pdf';
2674

2775
// Act
2876
final String? result = validator.validate(value);
2977

3078
// Assert
3179
expect(result, equals(customErrorMessage));
3280
});
81+
82+
test('should return null when the value is null and null check is disabled',
83+
() {
84+
// Arrange
85+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
86+
const FileExtensionValidator validator = FileExtensionValidator(
87+
allowedExtensions,
88+
checkNullOrEmpty: false,
89+
);
90+
const String? value = null;
91+
92+
// Act
93+
final String? result = validator.validate(value);
94+
95+
// Assert
96+
expect(result, isNull);
97+
});
98+
99+
test('should return the default error message when the value is null', () {
100+
// Arrange
101+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
102+
const FileExtensionValidator validator =
103+
FileExtensionValidator(allowedExtensions);
104+
const String? value = null;
105+
106+
// Act
107+
final String? result = validator.validate(value);
108+
109+
// Assert
110+
expect(result, isNotNull);
111+
expect(
112+
result,
113+
equals(
114+
FormBuilderLocalizations.current
115+
.fileExtensionErrorText(allowedExtensions.join(', ')),
116+
),
117+
);
118+
});
119+
120+
test(
121+
'should return null when the value is an empty string and null check is disabled',
122+
() {
123+
// Arrange
124+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
125+
const FileExtensionValidator validator = FileExtensionValidator(
126+
allowedExtensions,
127+
checkNullOrEmpty: false,
128+
);
129+
const String value = '';
130+
131+
// Act
132+
final String? result = validator.validate(value);
133+
134+
// Assert
135+
expect(result, isNull);
136+
});
137+
138+
test(
139+
'should return the default error message when the value is an empty string',
140+
() {
141+
// Arrange
142+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
143+
const FileExtensionValidator validator =
144+
FileExtensionValidator(allowedExtensions);
145+
const String value = '';
146+
147+
// Act
148+
final String? result = validator.validate(value);
149+
150+
// Assert
151+
expect(result, isNotNull);
152+
expect(
153+
result,
154+
equals(
155+
FormBuilderLocalizations.current
156+
.fileExtensionErrorText(allowedExtensions.join(', ')),
157+
),
158+
);
159+
});
160+
161+
test('should return null when the value has a valid path with extension',
162+
() {
163+
// Arrange
164+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
165+
const FileExtensionValidator validator =
166+
FileExtensionValidator(allowedExtensions);
167+
const String value = 'path/to/image.jpg';
168+
169+
// Act
170+
final String? result = validator.validate(value);
171+
172+
// Assert
173+
expect(result, isNull);
174+
});
175+
176+
test('should return the default error message for invalid file path format',
177+
() {
178+
// Arrange
179+
const List<String> allowedExtensions = <String>['jpg', 'png', 'gif'];
180+
const FileExtensionValidator validator =
181+
FileExtensionValidator(allowedExtensions);
182+
const String value = 'invalidpath';
183+
184+
// Act & Assert
185+
expect(() => validator.validate(value), throwsA(isA<AssertionError>()));
186+
});
33187
});
34188
}

0 commit comments

Comments
 (0)