GithubHelp home page GithubHelp logo

rulevalidator's Introduction

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

rulevalidator's People

Contributors

enixma avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.