Skip to content

Enixma/RuleValidator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RuleValidator-Android

Usage

RuleValidator can be initialized 2 ways .

  1. 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")));
  1. 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")));
                

Validation Result when init validator with rule

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);

Validation Result when init validator with both rule and 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();

Custom Rule by creating a class .

(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"));

Custom Rule without creating a class

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);

About

Android - Validator with generic typed value and generic typed error

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors