Reputation: 13091
There is a codebase which is extensively using Google AutoValue to create value classes which do not adhere to the JavaBean conventions, i. e. instead of getFoo()
, the corresponding method would be named simply foo()
. Unfortunately, Hibernate Validator does only validate JavaBean getters or class properties out of the box, so validation of the AutoValue classes fails silently.
How can I customize Hibernate Validator to validate POJOs which do not adhere to the JavaBean conventions (i. e. using get
, set
, and is
/has
prefixes)?
HibernateValidatorAutoValueTest.java
import com.google.auto.value.AutoValue;
import org.hibernate.validator.constraints.NotBlank;
import org.junit.Test;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class HibernateValidatorAutoValueTest {
@Test
public void test() {
final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
final Validator validator = factory.getValidator();
final Bean validBean = Bean.create("Valid", 100);
assertTrue("Valid bean should not have constraint violations", validator.validate(validBean).isEmpty());
final Bean invalidBean = Bean.create("", -1);
assertFalse("Invalid bean should have constraint violations", validator.validate(invalidBean).isEmpty());
}
@AutoValue
public static abstract class Bean {
@NotBlank
public abstract String text();
@NotNull
@Min(42L)
public abstract Integer number();
public static Bean create(String text, Integer number) {
return new AutoValue_HibernateValidatorAutoValueTest_Bean(text, number);
}
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<prerequisites>
<maven>3.0.1</maven>
</prerequisites>
<groupId>org.example</groupId>
<artifactId>hibernate-validator-auto-value</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.4.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Upvotes: 1
Views: 1027
Reputation: 18990
That's not possible right now, Bean Validation is centered around JavaBeans really. If you like, file an issue in our JIRA so we can explore it in the reference implementation but this will require quite some changes.
One possible workaround for the time being may be to use XML or the HV programmatic API for constraint declaration to apply constraints to the fields of the generated implementation of these types. This implies field access over getter access, though, and of course it's not as concise as putting constraints directly to the getter definitions of the auto-value types.
Upvotes: 2