Skip to content

Commit d165e67

Browse files
committed
Fixes
1 parent c500bc7 commit d165e67

11 files changed

+567
-100
lines changed

lib/src/bool/has_lowercase_chars_validator.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@ class HasLowercaseCharsValidator extends BaseValidator<String> {
2424
FormBuilderLocalizations.current.containsLowercaseCharErrorText(atLeast);
2525

2626
/// {@template lower_case_template}
27-
/// This regex matches any character that is not a lowercase letter (a-z) or ñ.
27+
/// This regex matches any character that is not a lowercase letter (a-z).
2828
///
2929
/// - It includes special characters, digits, and uppercase letters.
3030
/// - It can be used to find non-lowercase characters.
3131
///
3232
/// Examples: A, 1, @
3333
/// {@endtemplate}
34-
static final RegExp _lowerCase = RegExp('[^a-zñ]');
34+
static final RegExp _lowerCase = RegExp('[a-z]');
3535

3636
@override
3737
String? validateValue(String valueCandidate) {
38+
final int length = lowercaseCharLength(valueCandidate);
39+
3840
return lowercaseCharLength(valueCandidate) >= atLeast ? null : errorText;
3941
}
4042

4143
int lowercaseCharLength(String value) {
42-
return value.replaceAll(regex, '').length;
44+
return regex.allMatches(value).length;
4345
}
4446
}

lib/src/bool/has_numeric_chars_validator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class HasNumericCharsValidator extends BaseValidator<String> {
3131
///
3232
/// Examples: a, A, @
3333
/// {@endtemplate}
34-
static final RegExp _number = RegExp('[^0-9]');
34+
static final RegExp _number = RegExp('[0-9]');
3535

3636
@override
3737
String? validateValue(String valueCandidate) {
3838
return numberCharLength(valueCandidate) >= atLeast ? null : errorText;
3939
}
4040

4141
int numberCharLength(String value) {
42-
return value.replaceAll(regex, '').length;
42+
return regex.allMatches(value).length;
4343
}
4444
}

lib/src/bool/has_special_chars_validator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class HasSpecialCharsValidator extends BaseValidator<String> {
3131
///
3232
/// Examples: @, #, %
3333
/// {@endtemplate}
34-
static final RegExp _specialChar = RegExp('[A-Za-z0-9]');
34+
static final RegExp _specialChar = RegExp('[^A-Za-z0-9]');
3535

3636
@override
3737
String? validateValue(String valueCandidate) {
3838
return specialCharLength(valueCandidate) >= atLeast ? null : errorText;
3939
}
4040

4141
int specialCharLength(String value) {
42-
return value.replaceAll(regex, '').length;
42+
return regex.allMatches(value).length;
4343
}
4444
}

lib/src/bool/has_uppercase_chars_validator.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ class HasUppercaseCharsValidator extends BaseValidator<String> {
2424
FormBuilderLocalizations.current.containsUppercaseCharErrorText(atLeast);
2525

2626
/// {@template upper_case_template}
27-
/// This regex matches any character that is not an uppercase letter (A-Z) or Ñ.
27+
/// This regex matches any character that is not an uppercase letter (A-Z).
2828
///
2929
/// - It includes special characters, digits, and lowercase letters.
3030
/// - It can be used to find non-uppercase characters.
3131
///
3232
/// Examples: a, 1, @
3333
/// {@endtemplate}
34-
static final RegExp _upperCase = RegExp('[^A-ZÑ]');
34+
static final RegExp _upperCase = RegExp('[A-Z]');
3535

3636
@override
3737
String? validateValue(String valueCandidate) {
38-
return uppercaseCharLength(valueCandidate) >= 1 ? null : errorText;
38+
return uppercaseCharLength(valueCandidate) >= atLeast ? null : errorText;
3939
}
4040

4141
int uppercaseCharLength(String value) {
42-
return value.replaceAll(regex, '').length;
42+
return regex.allMatches(value).length;
4343
}
4444
}

test/src/bool/has_lowercase_chars_validator_test.dart

Lines changed: 141 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,62 @@ 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+
89
group('Has lowercase chars -', () {
10+
final HasLowercaseCharsValidator validator = HasLowercaseCharsValidator();
11+
912
test('should return null when the value has at least 1 lowercase character',
1013
() {
11-
final HasLowercaseCharsValidator validator = HasLowercaseCharsValidator();
12-
final String? result = validator.validate('Password123');
14+
// Arrange
15+
const String value = 'Password123';
16+
17+
// Act
18+
final String? result = validator.validate(value);
19+
20+
// Assert
1321
expect(result, isNull);
1422
});
1523

1624
test(
1725
'should return null when the value has at least 3 lowercase characters',
1826
() {
27+
// Arrange
1928
final HasLowercaseCharsValidator validator =
2029
HasLowercaseCharsValidator(atLeast: 3);
21-
final String? result = validator.validate('paSsword123');
30+
const String value = 'paSsword123';
31+
32+
// Act
33+
final String? result = validator.validate(value);
34+
35+
// Assert
2236
expect(result, isNull);
2337
});
2438

2539
test(
2640
'should return the custom error message when the value does not have any lowercase characters',
2741
() {
42+
// Arrange
2843
final HasLowercaseCharsValidator validator =
2944
HasLowercaseCharsValidator(errorText: customErrorMessage);
30-
final String? result = validator.validate('PASSWORD123');
45+
const String value = 'PASSWORD123';
46+
47+
// Act
48+
final String? result = validator.validate(value);
49+
50+
// Assert
3151
expect(result, equals(customErrorMessage));
3252
});
3353

3454
test(
3555
'should return the default error message when the value does not have any lowercase characters',
3656
() {
37-
final HasLowercaseCharsValidator validator = HasLowercaseCharsValidator();
38-
final String? result = validator.validate('PASSWORD123');
57+
// Arrange
58+
const String value = 'PASSWORD123';
59+
60+
// Act
61+
final String? result = validator.validate(value);
62+
63+
// Assert
3964
expect(
4065
result,
4166
equals(
@@ -44,74 +69,124 @@ void main() {
4469
);
4570
});
4671

47-
test(
48-
'should return null when the value has exactly 1 lowercase character',
49-
() {
50-
final HasLowercaseCharsValidator validator =
51-
HasLowercaseCharsValidator();
52-
final String? result = validator.validate('Password');
53-
expect(result, isNull);
54-
},
55-
);
72+
test('should return null when the value has exactly 1 lowercase character',
73+
() {
74+
// Arrange
75+
const String value = 'Password';
5676

57-
test(
58-
'should return the custom error message when the value does not have enough lowercase characters',
59-
() {
60-
final HasLowercaseCharsValidator validator = HasLowercaseCharsValidator(
61-
atLeast: 2,
62-
errorText: customErrorMessage,
63-
);
64-
final String? result = validator.validate('PASSWORD');
65-
expect(result, equals(customErrorMessage));
66-
},
67-
);
77+
// Act
78+
final String? result = validator.validate(value);
79+
80+
// Assert
81+
expect(result, isNull);
82+
});
6883

6984
test(
70-
'should return the default error message when the value does not have enough lowercase characters',
71-
() {
72-
final HasLowercaseCharsValidator validator =
73-
HasLowercaseCharsValidator(atLeast: 2);
74-
final String? result = validator.validate('PASSWORD');
75-
expect(
76-
result,
77-
equals(
78-
FormBuilderLocalizations.current.containsLowercaseCharErrorText(2),
79-
),
80-
);
81-
},
82-
);
85+
'should return the custom error message when the value does not have enough lowercase characters',
86+
() {
87+
// Arrange
88+
final HasLowercaseCharsValidator validator =
89+
HasLowercaseCharsValidator(atLeast: 2, errorText: customErrorMessage);
90+
const String value = 'PASSWORD';
91+
92+
// Act
93+
final String? result = validator.validate(value);
94+
95+
// Assert
96+
expect(result, equals(customErrorMessage));
97+
});
8398

8499
test(
85-
'should return the default error message when the value is an empty string',
86-
() {
87-
final HasLowercaseCharsValidator validator =
88-
HasLowercaseCharsValidator();
89-
final String? result = validator.validate('');
90-
expect(
91-
result,
92-
FormBuilderLocalizations.current.containsLowercaseCharErrorText(1),
93-
);
94-
},
95-
);
100+
'should return the default error message when the value does not have enough lowercase characters',
101+
() {
102+
// Arrange
103+
final HasLowercaseCharsValidator validator =
104+
HasLowercaseCharsValidator(atLeast: 2);
105+
const String value = 'PASSWORD';
106+
107+
// Act
108+
final String? result = validator.validate(value);
109+
110+
// Assert
111+
expect(
112+
result,
113+
equals(
114+
FormBuilderLocalizations.current.containsLowercaseCharErrorText(2),
115+
),
116+
);
117+
});
96118

97119
test(
98-
'should return null when the value is an empty string',
99-
() {
100-
final HasLowercaseCharsValidator validator =
101-
HasLowercaseCharsValidator(checkNullOrEmpty: false);
102-
final String? result = validator.validate('');
103-
expect(result, isNull);
104-
},
105-
);
120+
'should return the default error message when the value is an empty string',
121+
() {
122+
// Arrange
123+
const String value = '';
124+
125+
// Act
126+
final String? result = validator.validate(value);
127+
128+
// Assert
129+
expect(
130+
result,
131+
FormBuilderLocalizations.current.containsLowercaseCharErrorText(1),
132+
);
133+
});
134+
135+
test('should return null when the value is an empty string', () {
136+
// Arrange
137+
final HasLowercaseCharsValidator validator =
138+
HasLowercaseCharsValidator(checkNullOrEmpty: false);
139+
const String value = '';
140+
141+
// Act
142+
final String? result = validator.validate(value);
143+
144+
// Assert
145+
expect(result, isNull);
146+
});
147+
148+
test('should return null when the value is null', () {
149+
// Arrange
150+
final HasLowercaseCharsValidator validator =
151+
HasLowercaseCharsValidator(checkNullOrEmpty: false);
152+
const String? value = null;
153+
154+
// Act
155+
final String? result = validator.validate(value);
156+
157+
// Assert
158+
expect(result, isNull);
159+
});
106160

107161
test(
108-
'should return null when the value is null',
109-
() {
110-
final HasLowercaseCharsValidator validator =
111-
HasLowercaseCharsValidator(checkNullOrEmpty: false);
112-
final String? result = validator.validate(null);
113-
expect(result, isNull);
114-
},
115-
);
162+
'should return the custom error message when using a custom regex and the value does not match',
163+
() {
164+
// Arrange
165+
final HasLowercaseCharsValidator validator = HasLowercaseCharsValidator(
166+
regex: RegExp('[a-z]'),
167+
errorText: customErrorMessage,
168+
);
169+
const String value = 'PASSWORD';
170+
171+
// Act
172+
final String? result = validator.validate(value);
173+
174+
// Assert
175+
expect(result, equals(customErrorMessage));
176+
});
177+
178+
test('should return null when using a custom regex and the value matches',
179+
() {
180+
// Arrange
181+
final HasLowercaseCharsValidator validator =
182+
HasLowercaseCharsValidator(regex: RegExp('[a-z]'));
183+
const String value = 'Password';
184+
185+
// Act
186+
final String? result = validator.validate(value);
187+
188+
// Assert
189+
expect(result, isNull);
190+
});
116191
});
117192
}

0 commit comments

Comments
 (0)