LengthComplexityRule example

In this port under Passay, I will show with example the purpose of “LengthComplexityRule” class.

In all my previous posts under Passay, I showed how a password will have one set of rules that should be evaluated to check whether the password is valid or not.

Whereas in real situation, the rules to be evaluated is decided based on the length of the password.

For example if the length of the password is between 2 and 4, we want to evaluate “AllowedCharacterRule” and “characterOccurrencesRule” rule and if the length is greater than or equal to 5 we want to evaluate “AllowedCharacterRule” rule.

For these situations we take help of “LengthComplexityRule” class.

Below is the complete main class showing how to use “LengthComplexityRule” class.

Main class

1  package defaultPackage;
2  
3  import org.passay.AllowedCharacterRule;
4  import org.passay.CharacterOccurrencesRule;
5  import org.passay.LengthComplexityRule;
6  import org.passay.PasswordData;
7  import org.passay.PasswordValidator;
8  import org.passay.Rule;
9  import org.passay.RuleResult;
10 
11 public class Example16 {
12     public static void main(String[] args) {
13         /*Interval pattern --> ^([(\[])(\d+),(\d+|\*)([)\]])$*/
14         char[] chArray1 = {'a', 'b', 'c', 'd', 'e'};
15         Rule allowedCharacterRule1 = new AllowedCharacterRule(chArray1);
16         Rule characterOccurrencesRule = new CharacterOccurrencesRule(3);
17         
18         char[] chArray2 = {'v', 'w', 'x', 'y', 'z'};
19         AllowedCharacterRule allowedCharacterRule2 = new AllowedCharacterRule(chArray2);
20         
21         LengthComplexityRule lengthComplexityRule = new LengthComplexityRule();
22         lengthComplexityRule.addRules("[2,4]", allowedCharacterRule1, characterOccurrencesRule);
23         lengthComplexityRule.addRules("[5,*]", allowedCharacterRule2);
24         
25         PasswordValidator passwordValidator = new PasswordValidator(lengthComplexityRule);
26         
27         PasswordData passwordData = new PasswordData("aabb");
28         RuleResult ruleResult = passwordValidator.validate(passwordData);
29         System.out.println(ruleResult.isValid());
30         
31         passwordData = new PasswordData("vwxyz");
32         ruleResult = passwordValidator.validate(passwordData);
33         System.out.println(ruleResult.isValid());
34     }
35 }

In the above code, at line 15, I create an instance of “AllowedCharacterRule” class named “allowedCharacterRule1”. At line 16, I create an instance of “CharacterOccurrencesRule” class named “characterOccurrencesRule”. These rules will be evaluated when the length of the String is between 2 and 4.

At line 19, I create an another instance of “AllowedCharacterRule” class named “allowedCharacterRule2” which will be evaluated only when the password length is equal to or greater than 5.

So if the password length is between 2 and 4, two rules will be evaluated, whereas if the password length is 5 or greater, only one rule will be evaluated.

To configure that we take help of “LengthComplexityRule” class.

At line 21, I create an instance of “LengthComplexityRule” class.

At line 22 and 23, I add the rules by calling the “addRules” method available on “LengthComplexityRule” instance.

The arguments to “addRules” method are
1) the length for which the rule has to be evaluated.
2) the list of rules that have to executed if the correspoding length matches.

The we create two passwords and evaluate them.

In this way we can use “LengthComplexityRule” rule class.

Leave a comment