Constraints as rulii Rules
Every constraint, @Expr and @Validate declaration becomes a rulii rule the engine runs: a named, described, categorised and tagged rule that appears in the trace and can be listed from the registry. This page is about what the library builds and how to get at it.
What a Materialized Rule Says
Section titled “What a Materialized Rule Says”Every materialized rule says what it checks and where it was declared, carries the bean class as its category, and is tagged constraint, expr or hosted, the annotation’s name, and its groups. Its SourceDefinition names the field, method or constructor it was declared on.
code size must be between 2 and 5 (@Size on com.acme.Order.code)reference must not be null (@NotNull on com.acme.Order.reference)end must satisfy #{start == null || end.isAfter(start)} (@Expr on com.acme.Trip.end)The description comes from rulii 2.2’s ValueValidationRule.describe(), which names the value and the rule’s own parameters, followed by the declaration in parentheses. The constraint’s attributes (min, max, regexp, …) are the rule’s extra violation parameters, so every violation carries them under the names the constraint gives them.
Reading the Rules of a Class
Section titled “Reading the Rules of a Class”The transformation is public. For a class you can get its rules and its per-class RuleFlow, a self-contained pipeline that binds the object and runs everything:
BeanRules rules = validator.getBeanRules(Order.class);
for (MaterializedRule rule : rules.getRules()) { System.out.println(rule.getName() + ": " + rule.getRule().getDescription());}
RuleFlow<?> flow = validator.getRuleFlow(Order.class);| Method | Returns |
|---|---|
getBeanDefinition(Class) |
The BeanDefinition: what was read off the class, properties, constraints, @Validate, @Expr and @Bind declarations, executables. |
getBeanRules(Class) |
The BeanRules: the materialized rules, by property (getPropertyRules()), by container element (getElementRules()) and at class level (getClassRules()), with getRules() for all of them and getRule(name) for one. |
getRuleFlow(Class) |
The per-class RuleFlow: binds the object’s properties, runs the rules group step by group step, cascades, collects violations. |
getExecutableRules(Class, Executable) |
The rules of one method or constructor. |
getParametersRuleFlow(Class, Executable), getReturnValueRuleFlow(Class, Executable) |
A method’s or constructor’s parameters flow and return-value flow. |
getBeanClasses() |
The classes this validator has read so far. |
BeanDefinition.isCandidate(Class) (static) |
Whether a class declares anything the validator would read, from annotations alone. Tools that discover beans use it with getBeanClasses(). |
A MaterializedRule also tells where it is placed (getPlacement()), which binding it reads (getBindingName()), its message template, whether it hosts a ConstraintValidator or is a composition of other rules, and whether it is active for a set of groups (isActive(RequestedGroups)). BeanRules.ruleSet(groups), propertyRuleSet(name, groups) and elementRuleSet(node, groups) assemble the active rules into a RuleSet on demand.
Embedding in Your Own Flow
Section titled “Embedding in Your Own Flow”The per-class flow is an ordinary RuleFlow. Run it as a step of a larger flow and the annotation rules share bindings, violations and trace with everything else in it:
RuleFlow<?> checkout = RuleFlow.builder() .name("checkout") .run(validator.getRuleFlow(Order.class)) .run(pricingRules) .build();Listing and Filtering
Section titled “Listing and Filtering”Because the rules are categorised under the bean class name and tagged, a registry that holds them can be listed with rulii 2.2’s RuleRegistry.getByCategory("com.acme.Order") and getByTags("constraint", "OnCreate"). With rulii-spring and the rulii explorer 1.1, the Beans section lists every validated class member by member, opens the rule built for each constraint, draws the cascades between beans and the @Validate rules they run, and flags a @Validate name nothing is registered under.
Mapping Your Own Annotation to Rules
Section titled “Mapping Your Own Annotation to Rules”A ConstraintMapper says how one constraint annotation becomes rules. The standard constraints are mapped this way, to rulii’s built-in validators, and you can map your own, without a ConstraintValidator, so that the annotation becomes a rule that is listed, traced and reusable outside validation:
public interface ConstraintMapper<A extends Annotation> { Class<A> getAnnotationType(); List<Class<?>> getSupportedTypes(); // the rule's SUPPORTED_TYPES, or empty for any List<Rule> map(A annotation, ConstraintTarget target);}The ConstraintTarget carries everything prepared for the occurrence: the function reading the value (value()), the names, error code, severity, message, source, category and tags. target.single(builder) applies them to a rulii validation rule builder, describes the rule and builds it. SimpleConstraintMapper wraps a lambda:
ConstraintMapper<Sku> skuMapper = new SimpleConstraintMapper<>( Sku.class, PatternValidationRule.SUPPORTED_TYPES, (annotation, target) -> target.single(Validators.pattern(target.value(), "[A-Z]{3}-\\d{4}")));
BeanValidator validator = BeanValidator.builder() .mapper(skuMapper) .build();A mapper registered for an annotation that already has one replaces the built-in mapping, so a standard constraint can be re-mapped too. A mapper may return several rules for one occurrence; they run in order and report under the same error code. For a cross-parameter constraint, getSupportedTargets() returns ValidationTarget.PARAMETERS and the target’s type is Object[].
How a Validation Runs
Section titled “How a Validation Runs”For every validation call the library assembles a rulii RuleFlow per class and runs it on a RuleContext built from the validator’s RuleContextOptions: the properties are bound by name, the class’s rules run group step by group step (constraints, then @Expr conditions, then @Validate rules), containers are walked with the value extractors, @Valid cascades into the flow of the referenced class, and every violation is collected with its property path. A Tracer on the options sees each rule as it runs, exactly as it would for rules of your own.