RuleViolations & Severity
When validation rules fail, they add RuleViolation objects to a RuleViolations container. This section explains how to work with violations and severity levels.
Severity Levels
Section titled “Severity Levels”- FATAL Critical error. Should not proceed any further.
- ERROR Serious error. Action must be taken to fix it.
- WARNING Potential issue. Should be addressed at some point.
- INFO Informational message. No action required.
Working with RuleViolations
Section titled “Working with RuleViolations”RuleViolations violations = new RuleViolations();
// Check for problemsviolations.isEmpty(); // true if no violationsviolations.hasErrors(); // true if any ERROR-level violationsviolations.hasFatalErrors(); // true if any FATAL-level violationsviolations.hasWarnings(); // true if any WARNING-level violationsviolations.hasInfoMessages(); // true if any INFO-level violations
// Check for FATAL or ERROR combinedviolations.hasSevereErrors(); // true if FATAL or ERROR exist
// Count by severitylong errorCount = violations.getErrorCount(Severity.ERROR);long fatalCount = violations.getErrorCount(Severity.FATAL);Accessing Individual Violations
Section titled “Accessing Individual Violations”// Get all violations as a listList<RuleViolation> all = violations.getViolations();
// Get by indexRuleViolation first = violations.getViolation(0);
// Total countint count = violations.size();
// Iteratefor (RuleViolation v : violations) { System.out.println(v.getRuleName()); // "NotNullValidationRule" System.out.println(v.getErrorCode()); // "notNullValidationRule.errorCode" System.out.println(v.getSeverity()); // ERROR System.out.println(v.getErrorMessage()); // "Value must not be null." System.out.println(v.getParameters()); // {} additional params}Parameter Values (new in 2.2.0)
Section titled “Parameter Values (new in 2.2.0)”A RuleViolation keeps its parameter values as given. The constructor accepts a Map<String, ?> (existing Map<String, String> callers are unchanged), getParameters() still returns the string form of each value, and getParameterValues() returns the values themselves — the validated value as an object, for example, rather than its toString(). RuleViolationBuilder passes raw values through.
Map<String, String> strings = v.getParameters(); // {value=2025-01-01, max=2024-12-31}Map<String, Object> values = v.getParameterValues(); // {value=LocalDate, max=LocalDate}
LocalDate invalid = (LocalDate) values.get("value");Adding Violations Manually
Section titled “Adding Violations Manually”You can add violations yourself in custom rules or otherwise-actions:
RuleViolations violations = new RuleViolations();
// Quick add (defaults to Severity.ERROR)violations.add("myRule", "error.001");
// With all detailsviolations.add("myRule", "error.002", Severity.WARNING, "Something looks off");
// Using the builderviolations.add(RuleViolation.builder().build( "myRule", "error.003", "Detailed error message"));Complete Example
Section titled “Complete Example”// Build a validation RuleSetRuleSet<?> ruleSet = RuleSet.builder() .with("UserValidation") .rule(Rule.builder().build(new NotNullValidationRule("name"))) .rule(Rule.builder().build(new EmailValidationRule("email"))) .build();
// Run with invalid dataRuleViolations violations = new RuleViolations();ruleSet.run(name -> null, email -> "not-an-email", ruleViolations -> violations);
// Check resultsif (violations.hasSevereErrors()) { System.out.println("Validation failed with " + violations.size() + " error(s):"); for (RuleViolation v : violations) { System.out.println(" [" + v.getSeverity() + "] " + v.getErrorMessage()); }}