Skip to content

Inline Conditions with @Expr

For an invariant between fields that no annotation expresses, @Expr attaches a script condition, compiled once when the class is first read. No validator class, no class-level annotation, no registration.

@ExprDefaults(lang = "jakarta-el")
public class Trip {
public LocalDate start;
@Expr(value = "#{start == null || end.isAfter(start)}", errorCode = "trip.end")
public LocalDate end;
@Expr(value = "#{seats < 10}", errorCode = "trip.seats.max", message = "too many seats: ${value}")
public Integer seats;
}

When the script is false, a violation with the given error code is reported at the property (or at the bean, for a class-level @Expr). A script that throws, or returns something other than a boolean, is an error, not a violation.

The script sees the same scope a @Validate rule does: sibling properties by name, $this, $root, $parent, $groups, every @Bind of the site, and on an executable the arguments by parameter name, $parameters or $returnValue. On a property the value itself is also $value.

public class Booking {
// class level: reads properties by name
@Expr(value = "#{checkOut.isAfter(checkIn)}", errorCode = "booking.dates")
public static class Dates { LocalDate checkIn; LocalDate checkOut; }
// a method: reads the arguments by parameter name
@Expr(value = "#{from.isBefore(to)}", errorCode = "booking.range")
public List<Slot> slots(LocalDate from, LocalDate to) { ... }
}
Attribute Meaning
value The script, a condition in the language of lang. Must not be blank.
lang The scripting language as registered with rulii’s ScriptProcessorManager (jakarta-el, js, groovy, java, …). Empty means the nearest @ExprDefaults, else the manager’s default language.
errorCode Required. The error code of the violation, and the message key it is resolved under when message is empty.
message A rulii template: ${value} and ${validatedValue} stand for the validated value. Empty means the message resolved under errorCode through the bundles (ValidationMessages, the configured resolver, the library’s defaults), else the rule’s default text.
severity The severity of the violation. ERROR by default.
groups When the condition runs, with a constraint’s semantics: none means Default.
applyTo On a method or constructor, PARAMETERS or RETURN_VALUE; IMPLICIT picks the arguments when there are any.
skipNull On a field, getter or record component, whether a null property value skips the script (the default, as constraints leave null to @NotNull) or runs it. At class level and on an executable the script always runs.

@Expr is repeatable, and accepted on classes, fields, getters, record components, methods and constructors.

The language is never guessed from the classpath. It comes, in order, from:

  1. lang on the annotation itself.
  2. The nearest @ExprDefaults(lang = ...) on the class, then on its package (package-info.java).
  3. The default declared on rulii’s ScriptProcessorManager: setDefaultLanguage(...), the system property rulii.script.defaultLanguage, or in Spring Boot the property of the same name in application.properties.

A class with no language declared anywhere fails to load with an ExprDeclarationException listing the ways to declare one.

package-info.java
@ExprDefaults(lang = "jakarta-el")
package com.acme.model;
import org.rulii.validation.beans.ExprDefaults;

The script engine is rulii’s. jakarta-el ships with rulii core (it needs an EL implementation such as Expressly on the classpath); any JSR-223 engine on the classpath (GraalJS, Groovy, …) and any factory registered on ScriptProcessorManager can be named, per expression or for a whole class or package. See Scripting Support for the engines and how bindings are reached in each.

// JavaScript (GraalJS)
@Expr(value = "end.isAfter(start)", lang = "js", errorCode = "trip.end")
public LocalDate end;
// Java (Janino)
@Expr(value = "seats != null && seats < 10", lang = "java", errorCode = "trip.seats.max")
public Integer seats;

In Jakarta EL a binding is reachable as a bare identifier (#{end.isAfter(start)}) or through the root object ctx; a binding named like an EL reserved word (empty, and) only in bracket form, #{ctx['empty']}. The same engines back script conditions in registry rules, so a rule attached with @Validate can be a script too, in a language your domain experts read.

An @Expr message is a rulii template rather than a Bean Validation one: ${validatedValue} and ${value} are the validated value, and the error code resolves through the same bundles a constraint message does (ValidationMessages.properties, the configured MessageResolver, the library’s defaults).

ValidationMessages.properties
trip.end=the trip must end after it starts
trip.seats.max=a trip seats at most 10 people, not ${value}

On the Jakarta side an @Expr violation is not a constraint: getConstraintDescriptor() and getMessageTemplate() return null for it, while the path, the invalid value and the message are as for any violation.