Validating data is a common task that occurs throughout an application, from the presentation layer to the persistence layer. Often the same validation logic is implemented in each layer, proving to be time consuming and errorprone. To avoid duplication of these validations in each layer, developers often bundle validation logic directly into the domain model, cluttering domain classes with validation code that is, in fact, metadata about the class itself.
This JSR defines a metadata model and API for JavaBean validation. The default metadata source is annotations, with the ability to override and extend the meta-data through the use of XML validation descriptors.
Built-in constraints
To use built-in constraints your project must import the javax.validation dependency. Maven:
<dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.0.0.GA</version> <type>jar</type> <scope>test</scope> </dependency>
Here are some built-in annotations:
- @Null
- @NotNull
- @AssertTrue
- @AssertFalse
- @Min
- @Max
- @DecimalMin
- @DecimalMax
- @Size - covers min and max
- @Digits
- @Past - must be a date in the past
- @Future - must be a date in the future
- @Pattern
Usage:
import javax.validation.constraints.Size; import javax.validation.constraints.NotNull; // ... @NotNull @Size(min = 3) public String getName() { return name; }UPDATE: @Min is for numbers only, my bad. This code now uses @Size.
Testing the validation
Here’s an example of unit testing constraints. You must add a Bean Validation implementation:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.0.2.GA</version> <type>jar</type> <scope>test</scope> </dependency>
And the code itself:
package me.m1key.jsf; import static org.junit.Assert.assertEquals; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import org.junit.BeforeClass; import org.junit.Test; public class DogsBeanTest { private static Validator validator; @BeforeClass public static void setUp() { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); validator = factory.getValidator(); } @Test public void test() { DogsBean bean = new DogsBean(); Set<ConstraintViolation<DogsBean>> constraintViolations = validator .validate(bean); assertEquals(1, constraintViolations.size()); } }It could be a lot better, because I only check the size of the violations set here. You might check the actual error message (still not so elegant...).
Error messages
Error messages are customizable. They can be modified if you provide a ResourceBundle called ValidationMessages (so, you can create a ValidationMessages .properties file and make if available in the classpath root).
When does validation occur?
Very good question! The specification doesn’t explicitly say when; instead it provides a clue in form of Appendices D, E and F.
Different JEE technologies launch Bean Validation. But when?
JPA calls validation upon:
- pre-persist
- pre-update
- pre-remove
JSF calls validation upon:
- the process validation phase
Next article
In the next article I will show you how to define custom constraints and more.
Thanks.
ReplyDeleteFor reliable Java applications, use JEE 6 (JSR-303) for Master Bean Validation. Gain an understanding of data validation strategies to improve maintainability and performance. This was an easy-to-read and well-written post. I can't wait to read more content like to this in the future.
ReplyDeleteAbogado Conducir Sin Licencia Nueva Jersey
In Texas, cataract treatment is accessible and highly advanced, offering solutions for restoring clear vision affected by this common age-related condition. The treatment usually begins with a thorough eye examination to assess the extent of cataract development. Modern cataract surgery involves removing the clouded lens and replacing it with an artificial intraocular lens (IOL), which can even correct vision issues like nearsightedness or astigmatism.
ReplyDeleteLeading eye care centers across Texas provide laser-assisted surgery, which enhances precision and recovery times. Most surgeries are outpatient, allowing patients to return home the same day. With minimal discomfort and high success rates, cataract treatment Texas ensures improved vision and quality of life.