Skip to content

Commit 8f77883

Browse files
committed
Fixes
1 parent acbb489 commit 8f77883

File tree

4 files changed

+236
-4
lines changed

4 files changed

+236
-4
lines changed

lib/src/file/file_size_validator.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import '../base_validator.dart';
88
class FileSizeValidator extends BaseValidator<String> {
99
const FileSizeValidator(
1010
this.maxSize, {
11+
this.base1024Conversion = true,
12+
1113
/// {@macro base_validator_error_text}
1214
super.errorText,
1315

@@ -17,6 +19,9 @@ class FileSizeValidator extends BaseValidator<String> {
1719

1820
final int maxSize;
1921

22+
/// Whether to use base 1024 (true) or base 1000 (false) for the conversion.
23+
final bool base1024Conversion;
24+
2025
@override
2126
String get translatedErrorText => FormBuilderLocalizations.current
2227
.fileSizeErrorText(formatBytes(0), formatBytes(maxSize));
@@ -31,13 +36,13 @@ class FileSizeValidator extends BaseValidator<String> {
3136
}
3237

3338
/// Helper function to format bytes into a human-readable string (e.g., KB, MB, GB).
34-
String formatBytes(int bytes, {bool base1024 = true}) {
39+
String formatBytes(int bytes) {
3540
double log10(num x) => log(x) / ln10;
3641

3742
if (bytes <= 0) return '0 B';
3843

39-
final int base = base1024 ? 1024 : 1000;
40-
final List<String> units = base1024
44+
final int base = base1024Conversion ? 1024 : 1000;
45+
final List<String> units = base1024Conversion
4146
? <String>['B', 'KiB', 'MiB', 'GiB', 'TiB']
4247
: <String>['B', 'kB', 'MB', 'GB', 'TB'];
4348

lib/src/form_builder_validators.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,11 +768,13 @@ class FormBuilderValidators {
768768
/// - [checkNullOrEmpty] Whether to check for null or empty values.
769769
static FormFieldValidator<String> fileSize(
770770
int maxSize, {
771+
bool base1024Conversion = true,
771772
String? errorText,
772773
bool checkNullOrEmpty = true,
773774
}) =>
774775
FileSizeValidator(
775776
maxSize,
777+
base1024Conversion: base1024Conversion,
776778
errorText: errorText,
777779
checkNullOrEmpty: checkNullOrEmpty,
778780
).validate;

test/src/file/file_size_validator_test.dart

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,230 @@ void main() {
191191
// Assert
192192
expect(result, equals(customErrorMessage));
193193
});
194+
195+
group('FileSizeValidator with base1024Conversion set to false -', () {
196+
test('should return null when the file size is equal to the maximum size',
197+
() {
198+
// Arrange
199+
const int maxSize = 1000;
200+
const FileSizeValidator validator = FileSizeValidator(
201+
maxSize,
202+
base1024Conversion: false,
203+
);
204+
const String value = '1000';
205+
206+
// Act
207+
final String? result = validator.validate(value);
208+
209+
// Assert
210+
expect(result, isNull);
211+
});
212+
213+
test(
214+
'should return null when the file size is less than the maximum size',
215+
() {
216+
// Arrange
217+
const int maxSize = 1000;
218+
const FileSizeValidator validator = FileSizeValidator(
219+
maxSize,
220+
base1024Conversion: false,
221+
);
222+
const String value = '512';
223+
224+
// Act
225+
final String? result = validator.validate(value);
226+
227+
// Assert
228+
expect(result, isNull);
229+
});
230+
231+
test(
232+
'should return the default error message when the file size is greater than the maximum size',
233+
() {
234+
// Arrange
235+
const int maxSize = 1000;
236+
const FileSizeValidator validator = FileSizeValidator(
237+
maxSize,
238+
base1024Conversion: false,
239+
);
240+
const String value = '2000';
241+
242+
// Act
243+
final String? result = validator.validate(value);
244+
245+
// Assert
246+
expect(result, isNotNull);
247+
expect(
248+
result,
249+
equals(
250+
FormBuilderLocalizations.current.fileSizeErrorText('0 B', '1 kB'),
251+
),
252+
);
253+
});
254+
255+
test(
256+
'should return the custom error message when the file size is greater than the maximum size',
257+
() {
258+
// Arrange
259+
const int maxSize = 1000;
260+
final FileSizeValidator validator = FileSizeValidator(
261+
maxSize,
262+
base1024Conversion: false,
263+
errorText: customErrorMessage,
264+
);
265+
const String value = '2000';
266+
267+
// Act
268+
final String? result = validator.validate(value);
269+
270+
// Assert
271+
expect(result, equals(customErrorMessage));
272+
});
273+
274+
test(
275+
'should return null when the value is null and null check is disabled',
276+
() {
277+
// Arrange
278+
const int maxSize = 1000;
279+
const FileSizeValidator validator = FileSizeValidator(
280+
maxSize,
281+
base1024Conversion: false,
282+
checkNullOrEmpty: false,
283+
);
284+
const String? value = null;
285+
286+
// Act
287+
final String? result = validator.validate(value);
288+
289+
// Assert
290+
expect(result, isNull);
291+
});
292+
293+
test('should return the default error message when the value is null',
294+
() {
295+
// Arrange
296+
const int maxSize = 1000;
297+
const FileSizeValidator validator = FileSizeValidator(
298+
maxSize,
299+
base1024Conversion: false,
300+
);
301+
const String? value = null;
302+
303+
// Act
304+
final String? result = validator.validate(value);
305+
306+
// Assert
307+
expect(result, isNotNull);
308+
expect(
309+
result,
310+
equals(
311+
FormBuilderLocalizations.current.fileSizeErrorText('0 B', '1 kB'),
312+
),
313+
);
314+
});
315+
316+
test(
317+
'should return null when the value is an empty string and null check is disabled',
318+
() {
319+
// Arrange
320+
const int maxSize = 1000;
321+
const FileSizeValidator validator = FileSizeValidator(
322+
maxSize,
323+
base1024Conversion: false,
324+
checkNullOrEmpty: false,
325+
);
326+
const String value = '';
327+
328+
// Act
329+
final String? result = validator.validate(value);
330+
331+
// Assert
332+
expect(result, isNull);
333+
});
334+
335+
test(
336+
'should return the default error message when the value is an empty string',
337+
() {
338+
// Arrange
339+
const int maxSize = 1000;
340+
const FileSizeValidator validator = FileSizeValidator(
341+
maxSize,
342+
base1024Conversion: false,
343+
);
344+
const String value = '';
345+
346+
// Act
347+
final String? result = validator.validate(value);
348+
349+
// Assert
350+
expect(result, isNotNull);
351+
expect(
352+
result,
353+
equals(
354+
FormBuilderLocalizations.current.fileSizeErrorText('0 B', '1 kB'),
355+
),
356+
);
357+
});
358+
359+
test('should return null for a valid file size within the allowed range',
360+
() {
361+
// Arrange
362+
const int maxSize = 1000;
363+
const FileSizeValidator validator = FileSizeValidator(
364+
maxSize,
365+
base1024Conversion: false,
366+
);
367+
const String value = '500';
368+
369+
// Act
370+
final String? result = validator.validate(value);
371+
372+
// Assert
373+
expect(result, isNull);
374+
});
375+
376+
test(
377+
'should return the default error message for invalid file size format',
378+
() {
379+
// Arrange
380+
const int maxSize = 1000;
381+
const FileSizeValidator validator = FileSizeValidator(
382+
maxSize,
383+
base1024Conversion: false,
384+
);
385+
const String value = 'invalid-size';
386+
387+
// Act
388+
final String? result = validator.validate(value);
389+
390+
// Assert
391+
expect(result, isNotNull);
392+
expect(
393+
result,
394+
equals(
395+
FormBuilderLocalizations.current.fileSizeErrorText('0 B', '1 kB'),
396+
),
397+
);
398+
});
399+
400+
test(
401+
'should return the custom error message for invalid file size format',
402+
() {
403+
// Arrange
404+
const int maxSize = 1000;
405+
final FileSizeValidator validator = FileSizeValidator(
406+
maxSize,
407+
base1024Conversion: false,
408+
errorText: customErrorMessage,
409+
);
410+
const String value = 'invalid-size';
411+
412+
// Act
413+
final String? result = validator.validate(value);
414+
415+
// Assert
416+
expect(result, equals(customErrorMessage));
417+
});
418+
});
194419
});
195420
}

test/src/numeric/numeric_validator_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void main() {
128128
expect(
129129
result,
130130
equals(
131-
FormBuilderLocalizations.current.numericErrorText,
131+
validator.errorText,
132132
),
133133
);
134134
});

0 commit comments

Comments
 (0)