Skip to content

Commit

Permalink
Fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
themisir committed Apr 10, 2021
1 parent 1dea4a3 commit 9783094
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
8 changes: 3 additions & 5 deletions lib/src/validator_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class ValidationBuilder {
// Unless a builder is optional, the first thing we do is to add a
// [required] validator. All subsequent validators should expect
// a non-null argument.
if (optional != true) {
required(requiredMessage);
}
if (!optional) required(requiredMessage);
}

static FormValidatorLocale globalLocale = createLocale('default');
Expand Down Expand Up @@ -65,7 +63,7 @@ class ValidationBuilder {
String? test(String? value) {
for (var validate in validations) {
// Return null if field is optional and value is null
if (optional && value == null) {
if (optional && (value == null || value.isEmpty)) {
return null;
}

Expand Down Expand Up @@ -118,7 +116,7 @@ class ValidationBuilder {

/// Value must not be null
ValidationBuilder required([String? message]) =>
add((v) => (v == null || v == '') ? message ?? _locale.required() : null);
add((v) => v == null || v.isEmpty ? message ?? _locale.required() : null);

/// Value length must be greater than or equal to [minLength]
ValidationBuilder minLength(int minLength, [String? message]) =>
Expand Down
8 changes: 4 additions & 4 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import 'package:form_validator/form_validator.dart';

void checkValidation(
StringValidationCallback validate, {
List<String>? validValues,
List<String>? invalidValues,
List<String?> validValues = const [],
List<String?> invalidValues = const [],
}) {
if (validValues != null) {
if (validValues.isNotEmpty) {
validValues.forEach((value) {
expect(validate(value), isNull, reason: '"$value" is valid value');
});
}

if (invalidValues != null) {
if (invalidValues.isNotEmpty) {
invalidValues.forEach((value) {
expect(validate(value), isNotNull, reason: '"$value" is invalid value');
});
Expand Down
9 changes: 8 additions & 1 deletion test/validation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:form_validator/form_validator.dart';
import 'test_utils.dart';

void main() {

test('null', () {
final validate = ValidationBuilder().url().build();
expect(validate(null), equals("The field is required"));
Expand All @@ -16,6 +15,14 @@ void main() {
expect(validate(null), equals(null));
});

test('required', () {
final validate = ValidationBuilder(optional: false).required().build();

checkValidation(validate,
validValues: ['sa', ' ', ' ', 'some longest value'],
invalidValues: [null, '']);
});

test('validate min length', () {
final validate = ValidationBuilder().minLength(5).build();

Expand Down

0 comments on commit 9783094

Please sign in to comment.