Skip to content

Commit

Permalink
Lowercase support (#27)
Browse files Browse the repository at this point in the history
* lowercase support, readme updated & added lowercase prop in example

* lowercase tests created & changed hasMinLowercaseChar function to hasMinLowercase

* Feat: support of validating minimum lowercase characters (PR 27)

---------

Co-authored-by: Bruno Pereira <bruno.pereira@brudam.com.br>
Co-authored-by: Aref Mozafari <arefmozafari.78@gmail.com>
  • Loading branch information
3 people committed May 17, 2023
1 parent 39113a8 commit 97feec5
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## [1.6.0] - 2022-5-17

* New Feature: Validation for lowercase letters. Thanks to [BrunoPereira](https://github.com/BrunoPereira-2331)

## [1.5.0] - 2022-12-08

* New Feature: Add Key property to access to the validate function (See the example)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -37,6 +37,7 @@ new FlutterPwValidator(
controller: _passwordController,
minLength: 6,
uppercaseCharCount: 2,
lowercaseCharCount: 2,
numericCharCount: 3,
specialCharCount: 1,
width: 400,
Expand All @@ -51,6 +52,7 @@ new FlutterPwValidator(
| controller | Takes your password TextField controller | null |Yes|
| minLength | Takes total minimum length of password | null |Yes|
| uppercaseCharCount | Takes minimum uppercase character count that has to include in the password | 0 |No|
| lowercaseCharCount | Takes minimum lowercase character count that has to include in the password | 0 |No|
| numericCharCount | Takes minimum numeric character count that has to include in the password | 0 |No|
| specialCharCount | Takes minimum special character count that has to include in the password | 0 |No|
| width | Takes the widget width | null |Yes|
Expand Down
5 changes: 4 additions & 1 deletion example/ios/Runner.xcodeproj/project.pbxproj
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -171,10 +171,12 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand All @@ -185,6 +187,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down
2 changes: 2 additions & 0 deletions example/ios/Runner/Info.plist
Expand Up @@ -43,5 +43,7 @@
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
7 changes: 5 additions & 2 deletions example/lib/main.dart
Expand Up @@ -40,7 +40,8 @@ class AppHome extends StatelessWidget {
new Flexible(flex: 5, child: new FlutterLogo(size: 200)),
Flexible(
flex: 7,
child: new Column(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
Expand All @@ -60,11 +61,12 @@ class AppHome extends StatelessWidget {
controller: controller,
minLength: 8,
uppercaseCharCount: 2,
lowercaseCharCount: 3,
numericCharCount: 3,
specialCharCount: 1,
normalCharCount: 3,
width: 400,
height: 150,
height: 200,
onSuccess: () {
print("MATCHED");
ScaffoldMessenger.of(context).showSnackBar(new SnackBar(
Expand All @@ -76,6 +78,7 @@ class AppHome extends StatelessWidget {
),
],
),
)
)
],
),
Expand Down
1 change: 1 addition & 0 deletions lib/Resource/Strings.dart
Expand Up @@ -3,6 +3,7 @@ class FlutterPwValidatorStrings {
final String atLeast = "At least - character";
final String normalLetters = "- Letter";
final String uppercaseLetters = "- Uppercase letter";
final String lowercaseLetters = "- Lowercase letter";
final String numericCharacters = "- Numeric character";
final String specialCharacters = "- Special character";
}
3 changes: 2 additions & 1 deletion lib/Utilities/ConditionsHelper.dart
Expand Up @@ -9,12 +9,13 @@ class ConditionsHelper {
Map<String, bool>? _selectedCondition;

/// Recognize user selected condition from widget constructor to put them on map with their value
void setSelectedCondition(int minLength, normalCharCount, uppercaseCharCount,
void setSelectedCondition(int minLength, normalCharCount, uppercaseCharCount, lowercaseCharCount,
numericCharCount, specialCharCount) {
_selectedCondition = {
if (minLength > 0) strings.atLeast: false,
if (normalCharCount > 0) strings.normalLetters: false,
if (uppercaseCharCount > 0) strings.uppercaseLetters: false,
if (lowercaseCharCount > 0) strings.lowercaseLetters: false,
if (numericCharCount > 0) strings.numericCharacters: false,
if (specialCharCount > 0) strings.specialCharacters: false
};
Expand Down
6 changes: 6 additions & 0 deletions lib/Utilities/Validator.dart
Expand Up @@ -18,6 +18,12 @@ class Validator {
return password.contains(new RegExp(pattern));
}

/// Checks if password has at least lowercaseCount lowercase letter matches
bool hasMinLowercase(String password, int lowercaseCount) {
String pattern = '^(.*?[a-z]){' + lowercaseCount.toString() + ',}';
return password.contains(new RegExp(pattern));
}

/// Checks if password has at least numericCount numeric character matches
bool hasMinNumericChar(String password, int numericCount) {
String pattern = '^(.*?[0-9]){' + numericCount.toString() + ',}';
Expand Down
13 changes: 13 additions & 0 deletions lib/flutter_pw_validator.dart
Expand Up @@ -14,6 +14,7 @@ class FlutterPwValidator extends StatefulWidget {
final int minLength,
normalCharCount,
uppercaseCharCount,
lowercaseCharCount,
numericCharCount,
specialCharCount;
final Color defaultColor, successColor, failureColor;
Expand All @@ -31,6 +32,7 @@ class FlutterPwValidator extends StatefulWidget {
required this.onSuccess,
required this.controller,
this.uppercaseCharCount = 0,
this.lowercaseCharCount = 0,
this.numericCharCount = 0,
this.specialCharCount = 0,
this.normalCharCount = 0,
Expand Down Expand Up @@ -61,6 +63,7 @@ class FlutterPwValidatorState extends State<FlutterPwValidator> {
dynamic _hasMinLength,
_hasMinNormalChar,
_hasMinUppercaseChar,
_hasMinLowercaseChar,
_hasMinNumericChar,
_hasMinSpecialChar;

Expand Down Expand Up @@ -92,6 +95,13 @@ class FlutterPwValidatorState extends State<FlutterPwValidator> {
widget.translatedStrings.uppercaseLetters,
_hasMinUppercaseChar);

_hasMinLowercaseChar = _conditionsHelper.checkCondition(
widget.lowercaseCharCount,
_validator.hasMinLowercase,
widget.controller,
widget.translatedStrings.lowercaseLetters,
_hasMinLowercaseChar);

_hasMinNumericChar = _conditionsHelper.checkCondition(
widget.numericCharCount,
_validator.hasMinNumericChar,
Expand Down Expand Up @@ -136,6 +146,7 @@ class FlutterPwValidatorState extends State<FlutterPwValidator> {
widget.minLength,
widget.normalCharCount,
widget.uppercaseCharCount,
widget.lowercaseCharCount,
widget.numericCharCount,
widget.specialCharCount);

Expand Down Expand Up @@ -191,6 +202,8 @@ class FlutterPwValidatorState extends State<FlutterPwValidator> {
value = widget.normalCharCount;
if (entry.key == widget.translatedStrings.uppercaseLetters)
value = widget.uppercaseCharCount;
if (entry.key == widget.translatedStrings.lowercaseLetters)
value = widget.lowercaseCharCount;
if (entry.key == widget.translatedStrings.numericCharacters)
value = widget.numericCharCount;
if (entry.key == widget.translatedStrings.specialCharacters)
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,6 +1,6 @@
name: flutter_pw_validator
description: Flutter Pw Validator package helps you to validate sign in user-entered password with your rules.
version: 1.5.0
version: 1.6.0
homepage: https://github.com/ArefMozafari/flutter_pw_validator
maintainer: Aref Mozafari (@ArefMozafari)

Expand Down
10 changes: 6 additions & 4 deletions test/Utilities/ConditionHelper_test.dart
Expand Up @@ -16,34 +16,36 @@ void main() {
int minLength = 10;
int normalCharCount = 1;
int uppercaseCharCount = 0;
int lowercaseCharCount = 0;
int numericCharCount = 1;
int specialCharCount = 0;

int expectedResult = 3;

// act
conditionsHelper.setSelectedCondition(minLength, normalCharCount,
uppercaseCharCount, numericCharCount, specialCharCount);
uppercaseCharCount, lowercaseCharCount, numericCharCount, specialCharCount);
int actualResult = conditionsHelper.getter()!.length;

// assert
expect(actualResult, expectedResult);
});

test('Should _selectedCondition.length==5 when we have 5 condition > 0',
test('Should _selectedCondition.length==6 when we have 6 condition > 0',
() {
// arrange
int minLength = 10;
int normalCharCount = 3;
int uppercaseCharCount = 2;
int lowercaseCharCount = 2;
int numericCharCount = 2;
int specialCharCount = 2;

int expectedResult = 5;
int expectedResult = 6;

// act
conditionsHelper.setSelectedCondition(minLength, normalCharCount,
uppercaseCharCount, numericCharCount, specialCharCount);
uppercaseCharCount, lowercaseCharCount, numericCharCount, specialCharCount);
int actualResult = conditionsHelper.getter()!.length;

// assert
Expand Down
52 changes: 52 additions & 0 deletions test/Utilities/Validator_test.dart
Expand Up @@ -106,6 +106,58 @@ void main() {
expect(actualResult, expectedResult);
});
});
group("Tests for hasMinLowercase() component", () {
late Validator validator;

setUp(() {
validator = new Validator();
});

test(
'Should return True when lowercaseCount is 2 and password has 2 lowercase character',
() {
// arrange
String password = "abcDEF123";
int lowercaseCount = 3;
bool expectedResult = true;

// act
bool actualResult = validator.hasMinLowercase(password, lowercaseCount);

// assert
expect(actualResult, expectedResult);
});

test(
'Should return False when lowercaseCount is 5 and password has 3 lowercase character',
() {
// arrange
String password = "aBcDeFG123";
int lowercaseCount = 5;
bool expectedResult = false;

// act
bool actualResult = validator.hasMinLowercase(password, lowercaseCount);

// assert
expect(actualResult, expectedResult);
});

test(
'Should return True when lowercaseCount is 3 and password has 5 lowercase character',
() {
// arrange
String password = "abcdeFG123";
int lowercaseCount = 3;
bool expectedResult = true;

// act
bool actualResult = validator.hasMinLowercase(password, lowercaseCount);

// assert
expect(actualResult, expectedResult);
});
});
group("Test for hasMinNumericChar() component", () {
late Validator validator;
setUp(() {
Expand Down

0 comments on commit 97feec5

Please sign in to comment.