Spring Boot Integration
Add rulii-bean-validation beside rulii-spring 2.3.0 and it becomes the application’s Bean Validation provider, wired into the Spring rulii context. Adding the dependency is the switch; there is nothing else to configure.
<dependency> <groupId>org.rulii</groupId> <artifactId>rulii-bean-validation</artifactId> <version>1.1.0</version></dependency>It takes the place of Hibernate Validator, even when Hibernate Validator is still on the classpath. To turn it off while the dependency stays, set rulii.validation.enabled=false; Staying on Hibernate Validator has the details. Without the dependency rulii-spring leaves Bean Validation alone.
The library is not a certified implementation and does not run the TCK. The specification coverage page lists every known difference, and the migration guide says what a project coming from Hibernate Validator will notice.
What You Get
Section titled “What You Get”With this library and Spring Boot’s validation module on the classpath (what spring-boot-starter-validation brings), the auto-configuration org.rulii.spring.validation.BeanValidationConfig runs before Boot’s own validation auto-configuration and defines the defaultValidator bean Boot would otherwise define: a LocalValidatorFactoryBean pinned to RuliiValidationProvider. The provider is therefore selected whatever else is on the classpath, Hibernate Validator included.
- The
Validatorbean is rulii’s. Everything that validates through it,@Validon controller arguments,@Validatedservices through Boot’s method validation post-processor, does so unchanged. @Validatenames are bean names. The provider’s configuration receives the SpringRuleRegistry, so a rule, rule set or flow bean is attachable by its bean name.- The Spring rulii context applies. The Spring
RuleContextOptions(converters, message resolver, tracer, object factory) are the options validation runs with;ConstraintValidators are created as Spring beans with injection. - Mappers and extractors are beans. Every
ConstraintMapperandValueExtractorbean is registered on the provider. - Boot’s customizers still apply.
ValidationConfigurationCustomizerbeans run after the rulii wiring. - The unwrapped
BeanValidatoris a bean too, for code that wantsvalidateInor the materialized flows without unwrapping.
@Service@Validatedpublic class OrderService {
private final BeanValidator validator;
public OrderService(BeanValidator validator) { this.validator = validator; }
public Receipt place(@Valid Order order) { // method validation, through the rulii provider ... }
public RuleViolations check(RuleContext context, Order order) { return validator.validateIn(context, order); // inside a rulii context you own }}@Component("orderRules") // a @Validate("orderRules") namepublic class OrderRules { ... }Properties
Section titled “Properties”| Property | Default | Meaning |
|---|---|---|
rulii.validation.enabled |
true when rulii-bean-validation is on the classpath |
false turns rulii validation off while the dependency stays and keeps Hibernate Validator (see below). |
rulii.validation.failFast |
false |
Stop at the first violation. |
rulii.script.defaultLanguage |
none | The scripting language for @Expr and script rules that name none; read from the Spring Environment as rulii reads the system property of the same name. |
rulii.validation.failFast=truerulii.script.defaultLanguage=jakarta-elJakarta EL in @Expr and @Bind
Section titled “Jakarta EL in @Expr and @Bind”An @Expr or @Bind(expr = ...) in Jakarta EL can be written with either of EL’s delimiters, #{...} or ${...}. They evaluate the same. The examples throughout these pages use #{...}:
@ExprDefaults(lang = "jakarta-el")public class Trip {
public LocalDate start;
@Expr(value = "#{start == null || end.isAfter(start)}", errorCode = "trip.end") public LocalDate end;}rulii-spring resolves Spring property placeholders (${some.property}) in script text before it is compiled, but from rulii 2.3.0 it leaves Jakarta EL scripts as written, because ${...} is EL’s own syntax.
- A property placeholder inside a
jakarta-elscript is therefore not resolved. Pass the value in as a binding instead. ${value}and${validatedValue}in amessageare templates, not scripts, and work as documented.- Expressions in other languages (
js,java, SpEL asel) still have their${property}placeholders resolved.
Upgrading
Section titled “Upgrading”From rulii-spring 2.2. rulii-bean-validation no longer comes with rulii-spring. Add the dependency to keep validating with rulii; without it the application goes back to the validator Spring Boot sets up.
From rulii-spring 2.1 or earlier, or adding rulii validation for the first time. A Boot application that validated with Hibernate Validator validates with rulii once the dependency is added, with nothing else changed. Violations, paths and messages are meant to come out the same; the migration guide lists the differences.
Taking Hibernate Validator out is optional. While it is on the classpath, a library that calls Validation.buildDefaultValidatorFactory() itself gets whichever provider comes first, and JPA keeps using it (see JPA). To take it out, exclude hibernate-validator from the validation starter:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <exclusions> <exclusion> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> </exclusion> </exclusions></dependency>Keep the starter itself: it brings Boot’s validation wiring and an EL implementation, which ${...} in messages and @Expr in Jakarta EL need.
JPA is not affected. Spring Boot does not hand its Validator to JPA; Hibernate builds one of its own. While Hibernate Validator is on the classpath, JPA goes on validating entities with it, whatever the dependency order. So @Validate and @Expr on an entity are not checked at persist time: validate the entity through the Validator bean before saving it. An application that takes Hibernate Validator out has one provider left, and JPA validates with rulii.
Staying on Hibernate Validator
Section titled “Staying on Hibernate Validator”To keep Hibernate Validator while this library is on the classpath, set rulii.validation.enabled=false. HibernateValidatorConfig then defines the defaultValidator bean pinned to Hibernate Validator, with Boot’s message interpolator and customizers, so the opt-out is deterministic although two providers are on the classpath. Without Hibernate Validator on the classpath the opt-out leaves the bean to Boot, which bootstraps the one provider there is, rulii’s, without the Spring rulii wiring.
rulii.validation.enabled=falseYour Own Validator Bean
Section titled “Your Own Validator Bean”Both configurations are @ConditionalOnMissingBean(Validator.class): define a Validator bean of your own and neither runs. To keep the rulii wiring and still customise, prefer a ValidationConfigurationCustomizer bean, which receives the RuliiConfiguration after the Spring context has been applied:
@BeanValidationConfigurationCustomizer failFastInProduction(Environment env) { return configuration -> { if (configuration instanceof RuliiConfiguration rulii && env.acceptsProfiles(Profiles.of("prod"))) { rulii.failFast(true); } };}Explorer
Section titled “Explorer”The rulii explorer has a Beans section for applications that validate with rulii-spring’s validator: with rulii.explorer.beans.enabled=true it 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.