Skip to content

Commit 439fe0a

Browse files
update has min special chars
1 parent 0d4f167 commit 439fe0a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/new_api_prototype.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ Validator<String> hasMinUppercaseChars({
266266
String Function(int)? hasMinUppercaseCharsMsg,
267267
}) {
268268
assert(min > 0, 'min must be positive (at least 1)');
269+
269270
return (value) {
270271
var uppercaseCount = customUppercaseCounter?.call(value) ??
271272
_upperAndLowerCaseCounter(value).upperCount;
@@ -362,7 +363,12 @@ Validator<String> hasMinSpecialChars({
362363
}) {
363364
assert(min > 0, 'min must be positive (at least 1)');
364365
return (value) {
365-
return (regex ?? _specialRegex).allMatches(value).length >= min
366+
final (lowerCount: lowerCount, upperCount: upperCount) =
367+
_upperAndLowerCaseCounter(value);
368+
final specialCount = value.length -
369+
(lowerCount + upperCount + RegExp('[0-9]').allMatches(value).length);
370+
371+
return specialCount >= min
366372
? null
367373
: hasMinSpecialCharsMsg?.call(min) ??
368374
FormBuilderLocalizations.current.containsSpecialCharErrorText(min);

test/new_api_testing/bool/has_min_special_chars_validator_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import 'package:flutter_test/flutter_test.dart';
22
import 'package:form_builder_validators/form_builder_validators.dart';
33

44
void main() {
5+
String turkishLowercase = 'abcçdefgğhıijklmnoöprsştuüvyz';
6+
String randomAlphabeticalChars = 'bæàøĉijɣφϋϗϧѓѽծⴌḝἄⓜⰲⲫa';
57
const String customErrorMessage = 'custom error message';
68
group('Validator: hasMinSpecialCharsValidator', () {
79
group('Validations with default error message', () {
810
final List<({String input, bool isValid, int? minValue})> testCases =
911
<({int? minValue, String input, bool isValid})>[
1012
(minValue: null, input: '', isValid: false),
13+
(minValue: null, input: ' ', isValid: true),
1114
(minValue: null, input: 'D', isValid: false),
1215
(minValue: null, input: 'password', isValid: false),
1316
(minValue: 1, input: '*', isValid: true),
@@ -18,8 +21,13 @@ void main() {
1821
(minValue: 2, input: '==', isValid: true),
1922
(minValue: 2, input: '@_-', isValid: true),
2023
(minValue: 2, input: 'password13%%', isValid: true),
24+
(minValue: 2, input: 'paççword13', isValid: false),
2125
(minValue: 2, input: 'Password123#@#', isValid: true),
2226
(minValue: 4, input: 'Pass0word123!!!!', isValid: true),
27+
// Testing for non A-Z chars
28+
(minValue: 1, input: 'ç', isValid: false),
29+
(minValue: 1, input: randomAlphabeticalChars, isValid: false),
30+
(minValue: 1, input: turkishLowercase, isValid: false),
2331
];
2432

2533
for (final (

0 commit comments

Comments
 (0)