RuleValidator can be initialized 2 ways .
- Rule or Set of Rules .
// Ex.1 init with rule / set of rules
RuleValidator emailValidator = new RuleValidator(new StringEmailRule("input is not in email format"));
RuleValidator passwordValidator = new RuleValidator(new RuleSet()
.addRule(new StringMinLengthRule(6, "at least 6 characters"))
.addRule(new StringMaxLengthRule(10, "cannot exceed 10 characters")));
- Value ( to be validated ) and ( Rule or Set of Rules ) .
Result is ready upon initialization .
// Ex.2 init with (rule or set of rules) and value (to be validated)
RuleValidator emailValidator = new RuleValidator("text", new StringEmailRule("input is not in email format"));
RuleValidator passwordValidator = new RuleValidator("text", new RuleSet()
.addRule(new StringMinLengthRule(6, "at least 6 characters"))
.addRule(new StringMaxLengthRule(10, "cannot exceed 10 characters")));
Need to provide value to be validated .
isValid(T1 value) . validate value and return boolean .
boolean isValid = validator.isValid(value);
validate(T1 value) . validate value and return a list of error objects .
List<T2> errorObjects = validator.validate(value);
Able to get result right away
isValid() return result in boolean
boolean isValid = validator.isValid();
getErrors() return list of error objects
List<T2> errorObjects = validator.getErrors();
(See example in source code: StringMinLengthRule.java, StringMaxLengthRule.java, StringEmailRule.java) .
Your custom rule classes can be utilized like the example below .
Min Length Rule
new RuleValidator("text", new StringMinLengthRule(3, "at least 3 characters"));
Max Length Rule
new RuleValidator("text", new StringMaxLengthRule(5, "Exceeded 5 characters"));
Email Rule
new RuleValidator("text", new StringEmailRule("Incorrect email pattern"));
Rule customRule = new Rule<String, String>() {
@Override
public boolean isValid(String s) {
return s.equals("text");
}
@Override public String getError() {
return "text not matched";
}
};
new RuleValidator("text", customRule);