Skip to content

A simple and extensible validation utility for Java.

License

Notifications You must be signed in to change notification settings

fabiandev/java-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

java-validator

A simple and extensible validation utility for Java.

Usage

import io.fabiandev.validator.contracts.Validator;
import io.fabiandev.validator.contracts.Bag;
import io.fabiandev.validator.core.StandardValidator;

Validator validator = new StandardValidator(request);

validator
    .rule("email", "max:35|email")
    .rule("phone", "required_unless:email|min:9|digits")
        .message("phone.min", ":field must be at least :min characters long.")
        .message("phone.digits", "For :field only digits are allowed")
    .rule("name", "required|min:6")
        .message("name.min", "Please fill in your name with at least :min characters.")
    .rule("tos_accepted", "required")
        .message("tos_accepted.required", "Please accept the Terms of Service.");

if (validator.fails()) {
    System.out.println("There were errors!");
    Bag errorBag = validator.errors();
    return;
}

System.out.println("Validation passed!");

Extending

Find two basic examples of how to create custom rules below, and add them to the RulesManage

import io.fabiandev.validator.core.RulesManager;

RulesManager.addRule(MyRule.class);

Standard Rule

import io.fabiandev.validator.core.BaseRule;

public class MyRule extends BaseRule {

  @Override
    protected void validate() {
    // Get the input field's value: this.inputField.getValue()
    // Get all input fields: this.inputFields
    // Get the rule parameter: this.ruleValue
    // Call this.fail() if validation doesn't pass
    }

    @Override
    protected String defaultMessage() {
        return ":field can include :my_rule placeholders for the parameter.";
    }

}

Regex Rule

import io.fabiandev.validator.rules.Regex;

public class Everything extends Regex {

    @Override
    protected String regex() {
        return "^.*$";
    }

    @Override
    protected String defaultMessage() {
        return ":field did not pass the everything rule.";
    }

}

Full Example

import io.fabiandev.validator.core.BaseRule;

public class ExactNumber extends BaseRule {

  @Override
    protected void validate() { 
      int expected = Integer.parseInt(this.ruleValue);
      int actual = Integer.parseInt(this.inputField.getValue());
      
      if (actual != expected)
      {
          this.fail();
      }
    }

    @Override
    protected String defaultMessage() {
        return ":field must contain the number :exact_number.";
    }

}
import io.fabiandev.validator.core.RulesManager;

RulesManager.addRule(ExactNumber.class);
import io.fabiandev.validator.contracts.Validator;
import io.fabiandev.validator.core.StandardValidator;

Validator validator = new StandardValidator(data);

validator
    .rule("inputField", "exact_number:10|required")
    .messsage("inputField.exact_number", "Field ':field should' contain the number :exact_number.")
    

validator.validate();
validator.errors(); // contains "Field 'inputField' should contain the number 10."

About

A simple and extensible validation utility for Java.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages