Skip to content

Attaching rulii Rules with @Validate

@Validate names runnables in the validator’s RuleRegistry: a Rule, a RuleSet or a RuleFlow. They run in declaration order, after the constraints of the same group step, and report into the same violations. The bean names the rule; what runs under that name lives in the registry, where it is listed and swapped without touching the bean.

RuleRegistry registry = RuleRegistry.builder()
.register(Validators.notNull(Validators.binding("customer")).name("customerRequired").build())
.register(orderRules) // a RuleSet or RuleFlow of your own
.build();
@Validate({"customerRequired", "orderRules"})
public class Order {
@Validate("codeFormat")
String code;
@Validate(value = "auditTrail", groups = OnCreate.class)
void place(@NotNull Customer by) { ... }
}
BeanValidator validator = BeanValidator.builder().registry(registry).build();

A rule attached with @Validate receives the bean’s properties as rulii bindings, by name, so it declares parameters the way any rulii rule does. What is in scope depends on where the annotation sits:

Placement In scope Violations reported at
Class Every property by name, $this, $root, $parent, $groups The bean
Field, getter, record component The above and the property’s value as $value The property
Method or constructor The arguments by parameter name and as $parameters, or the return value as $value and $returnValue, as applyTo says The cross-parameter or return-value node
  • $this is the object being validated, $root the object the validation call started from, $parent the object that cascaded into this one (null at the root).
  • $groups is the RequestedGroups of the call: a rule that wants to branch on them reads it like any other binding.
  • A property the bean does not set is still bound, as null. A rule that cannot work without it says so in a @PreCondition, as any rulii rule would.
Attribute Meaning
value The registry names to run, in declaration order. At least one; a name must not be blank or repeat.
groups When the rules run, with the semantics of a constraint’s groups: none means Default. Every group must be an interface.
applyTo On a method or constructor, PARAMETERS or RETURN_VALUE. IMPLICIT (the default) means the arguments when the executable has parameters and the return value otherwise.

Names are resolved at validation time, so what is registered under a name can change without recompiling the bean. A name that resolves to nothing fails loudly with a ValidateRuleNotFoundException, never silently. @Validate is accepted on classes, fields, getters, record components, methods and constructors.

One registered rule, parameterised per site. @Bind is to a rule what max = 10 is to @Size: a fact about the declaration, bound by name beside the properties (or the arguments of a method) for every rule and script that runs there.

@Validate("withinLimit") @Bind(name = "limit", value = "2", type = Integer.class)
public class TableBooking { @NotNull Integer seats; }
@Validate("withinLimit") @Bind(name = "limit", value = "500", type = Integer.class)
public class HallBooking { @NotNull Integer seats; }
Rule.builder().name("withinLimit")
.given(condition((Integer seats, Integer limit) -> seats == null || seats <= limit))
.otherwise(action((RuleViolations violations) -> violations.add("withinLimit", "seats.limit")))
.build();
Attribute Meaning
name The binding name the rules read the value under. Must be a valid rulii name and must be free: a property’s, a parameter’s or a reserved name fails when the class is first read.
value A literal. Bound as a String, or converted once to type through the validator’s converters when one is declared. Exclusive with expr. A literal that does not convert fails when the class is read.
expr A script in the @Expr languages, evaluated lazily on the first read against the same scope, the other bindings of the site included: @Bind(name = "twice", expr = "#{base * 2}"). Exclusive with value.
type The type the value is bound with. Object (the default) means none declared. Any other type converts the literal, or checks the expression result against it.
lang The scripting language of expr. Empty means the nearest @ExprDefaults, else the manager’s default language.
applyTo On a method or constructor, whether the binding sits beside the arguments or the return value, following the inheritance rules of the constraints there.

@Bind is repeatable. Class-level bindings are inherited and a subclass can redeclare one. What @Bind is not for: a value that changes for a business reason, which belongs in the registry, and anything that reaches a service, which is the rule’s own wiring.

Within a group step the constraints run first, then the @Expr conditions, then the @Validate rules, each in declaration order. With a @GroupSequence a later group runs only if the earlier one produced no violations, rules included. With failFast the run stops at the first violation whatever produced it.