Skip to content

Commit

Permalink
Improve required validator joanpablo#377
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilich6107 committed May 9, 2023
1 parent 59fe334 commit 330a17b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/src/validators/required_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ class RequiredValidator extends Validator<dynamic> {
@override
Map<String, dynamic>? validate(AbstractControl<dynamic> control) {
final error = <String, dynamic>{ValidationMessage.required: true};
final dynamic value = control.value;

if (control.value == null) {
if (value == null) {
return error;
} else if (value is String) {
return value.trim().isEmpty ? error : null;
} else if (value is Iterable && value.isEmpty) {
return error;
} else if (value is Map && value.isEmpty) {
return error;
} else if (value is Set && value.isEmpty) {
return error;
} else if (control.value is String) {
return (control.value as String).trim().isEmpty ? error : null;
}

return null;
Expand Down
106 changes: 106 additions & 0 deletions test/src/validators/required_validator_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:reactive_forms/reactive_forms.dart';

void main() {
group('Required validator tests', () {
test('FormControl is invalid if value is null', () {
final control = FormControl<String>(
value: null,
validators: [Validators.required],
);

expect(control.valid, false);
expect(control.errors, {
ValidationMessage.required: true,
});
});

test('FormControl is invalid if value is empty string', () {
final control = FormControl<String>(
value: '',
validators: [Validators.required],
);

expect(control.valid, false);
expect(control.errors, {
ValidationMessage.required: true,
});
});

test('FormControl is valid if value is non empty string', () {
final control = FormControl<String>(
value: 'non empty string',
validators: [Validators.required],
);

expect(control.valid, true);
expect(control.errors, <String, dynamic>{});
});

test('FormControl is invalid if value is empty list', () {
final control = FormControl<List<String>>(
value: [],
validators: [Validators.required],
);

expect(control.valid, false);
expect(control.errors, {
ValidationMessage.required: true,
});
});

test('FormControl is valid if value is non empty list', () {
final control = FormControl<List<String>>(
value: ['non empty string'],
validators: [Validators.required],
);

expect(control.valid, true);
expect(control.errors, <String, dynamic>{});
});

test('FormControl is invalid if value is empty map', () {
final control = FormControl<Map<String, dynamic>>(
value: <String, dynamic>{},
validators: [Validators.required],
);

expect(control.valid, false);
expect(control.errors, {
ValidationMessage.required: true,
});
});

test('FormControl is valid if value is non empty map', () {
final control = FormControl<Map<String, String>>(
value: {'key': 'value'},
validators: [Validators.required],
);

expect(control.valid, true);
expect(control.errors, <String, dynamic>{});
});

test('FormControl is invalid if value is empty set', () {
final control = FormControl<Set<String>>(
value: <String>{},
validators: [Validators.required],
);

expect(control.valid, false);
expect(control.errors, {
ValidationMessage.required: true,
});
});

test('FormControl is valid if value is non empty set', () {
final control = FormControl<Set<String>>(
value: {'value'},
validators: [Validators.required],
);

expect(control.valid, true);
expect(control.errors, <String, dynamic>{});
});
});
}

0 comments on commit 330a17b

Please sign in to comment.