10 — Validation
Built-in Validation Rules
rulii ships with 39+ ready-to-use validation rules. Each one checks a specific constraint and adds a RuleViolation when the check fails.
Available Validation Rules
| Rule | What It Checks |
|---|---|
| NotNullValidationRule | Value is not null |
| NotEmptyValidationRule | String/Collection is not empty |
| NotBlankValidationRule | String is not blank (not null, not empty, not whitespace) |
| SizeValidationRule | String/Collection size is within range |
| MinValidationRule | Number ≥ minimum |
| MaxValidationRule | Number ≤ maximum |
| DecimalMinValidationRule | Decimal ≥ minimum |
| DecimalMaxValidationRule | Decimal ≤ maximum |
| EmailValidationRule | Valid email format |
| UrlValidationRule | Valid URL format |
| PatternValidationRule | Matches a regex pattern |
| AssertTrueValidationRule | Value is true |
| AssertFalseValidationRule | Value is false |
| PositiveValidationRule | Number > 0 |
| PositiveOrZeroValidationRule | Number ≥ 0 |
| NegativeValidationRule | Number < 0 |
| NegativeOrZeroValidationRule | Number ≤ 0 |
| FutureValidationRule | Date is in the future |
| FutureOrPresentValidationRule | Date is now or in the future |
| PastValidationRule | Date is in the past |
| PastOrPresentValidationRule | Date is now or in the past |
| DigitsValidationRule | Number has expected integer/fraction digits |
| LowerCaseValidationRule | String is all lowercase |
| UpperCaseValidationRule | String is all uppercase |
| AlphaValidationRule | String contains only letters |
| AlphaNumericValidationRule | String contains only letters and digits |
| NumericValidationRule | String contains only digits |
| StartsWithValidationRule | String starts with a prefix |
| EndsWithValidationRule | String ends with a suffix |
| ContainsValidationRule | String contains a substring |
Basic Usage
// Create a validation rule for a field called "name"
NotNullValidationRule notNull = new NotNullValidationRule("name");
// Wrap it as a Rule
Rule rule = Rule.builder().build(notNull);
// Run it
RuleViolations violations = new RuleViolations();
rule.run(name -> null, ruleViolations -> violations);
// Check for errors
violations.hasErrors(); // true — name was null
Collecting Violations
Validation rules automatically add violations to the ruleViolations binding. Pass a RuleViolations instance to collect errors:
RuleSet<?> ruleSet = RuleSet.builder()
.with("FormValidation")
.rule(Rule.builder().build(new NotNullValidationRule("name")))
.rule(Rule.builder().build(new SizeValidationRule("name", 2, 50)))
.rule(Rule.builder().build(new EmailValidationRule("email")))
.build();
RuleViolations violations = new RuleViolations();
ruleSet.run(
name -> "",
email -> "not-valid",
ruleViolations -> violations
);
for (RuleViolation v : violations) {
System.out.println(v.getErrorMessage());
}
Customizing Error Details
// Custom error code and severity
NotNullValidationRule rule = new NotNullValidationRule(
"name", // field name
"ERR-001", // error code
Severity.FATAL, // severity level
"Name is required!" // custom message
);