Bean Validation Module

Overview

The Bean Validation module provides CDI integration for bean validation. It enables the creation of CDI-aware ConstraintValidator methods that can use business objects (EJBs, ManagedBeans) to support validation needs.

Project Setup

The configuration information provided here is for Maven-based projects and it assumes that you have already declared the DeltaSpike version and DeltaSpike Core module for your projects, as detailed in Configure DeltaSpike in Your Projects. For Maven-independent projects, see Configure DeltaSpike in Maven-independent Projects.

Declare Bean Validation Module Dependencies

Add the Bean Validation module to the list of dependencies in the project pom.xml file using this code snippet:

<dependency>
    <groupId>org.apache.deltaspike.modules</groupId>
    <artifactId>deltaspike-bean-validation-module-impl</artifactId>
    <version>${deltaspike.version}</version>
    <scope>runtime</scope>
</dependency>

Or if you’re using Gradle, add these dependencies to your build.gradle:

     runtime 'org.apache.deltaspike.modules:deltaspike-bean-validation-module-impl'

Scoping

A ConstraintValidator inherits the scope defined in the bean class.

A ConstraintValidator may be invoked by multiple threads. For this reason, you should consider using at least RequestScoped validators.

Code Requirements

There are no compile dependencies to use the Bean Validation module. You simply need to override the factory. Then you can build your ConstraintValidator based on CDI programming rules.

Overriding in Java
Validation.byDefaultProvider().configure().constraintValidatorFactory(new CDIAwareConstraintValidatorFactory()).buildValidatorFactory()
Overriding in XML
<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">
    <constraint-validator-factory>org.apache.deltaspike.beanvalidation.impl.CDIAwareConstraintValidatorFactory</constraint-validator-factory>
</validation-config>