Help

I am happy to announce the first beta release of Hibernate Validator 4.2. You can download this release as usual from the JBoss Maven Repository or from SourceForge.

As announced earlier, the highlight of the 4.2 release is the implementation of method level constraints (HV-347) as described in Appendix C of the Bean Validation specification. Many thanks to Gunnar for implementing this new feature. Method level validation allows to apply constraints to method parameters or return values like this:

public @NotNull String saveItem(@Valid @NotNull Item item, @Max(23) BigDecimal price)
To validate these constraints you would get hold of a MethodValidator via the Validator.unwrap() method:
MethodValidator validator = Validation.byProvider( HibernateValidator.class )
    .configure()
    .buildValidatorFactory()
    .getValidator()
    .unwrap( MethodValidator.class );
and use one of the following methods of this new interface:
public interface MethodValidator {
    public <T> Set<MethodConstraintViolation<T>> validateParameter(T object, Method method, Object parameterValue, int parameterIndex, Class<?>... groups);
    public <T> Set<MethodConstraintViolation<T>> validateParameters(T object, Method method, Object[] parameterValues, Class<?>... groups);
    public <T> Set<MethodConstraintViolation<T>> validateReturnValue(T object, Method method, Object returnValue, Class<?>... groups);
}
The returned MethodConstraintViolation is derived from ConstraintViolation and provides additional method level validation specific information. For example it can contain the method name and index of the parameter which caused the constraint violation. The documentation for this feature is still work in progress and will be completed in the following releases.

We except that the new MethodValidator API is in particular interesting when used in conjunction with APIs or frameworks like Contexts and Dependency Injection (JSR-299) or aspect oriented frameworks. With the help of these frameworks it is very simple to create an interceptor/aspect which automatically triggers validation of all method invocations and throws a MethodConstraintViolationExcepion in case of any validation errors. Let us know how you are going to use this new API.

Next to method level validation we also introduced boolean composition for composed constraints (HV-390). So far composing constraints were always a conjunction of all specified constraints. With this release Hibernate Validator introduces the @ConstraintComposition annotation together with the CompositionType enum. This allows you to define composing constraints like this:

@ConstraintComposition(OR)
@Pattern(regexp = "K")
@Size(min = 2, max = 10)
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PatternOrSize {
	public abstract String message() default "OR";
	public abstract Class<?>[] groups() default { };
	public abstract Class<? extends Payload>[] payload() default { };
}
This is yet another contribution from the community. Thanks a lot for your support :)

Another new feature worth mentioning is the new ValueFormatterMessageInterpolator which allows you to interpolate the validated value in the message templates. To enable this new interpolator use the configuration API:

Validation.byProvider( HibernateValidator.class ).configure().messageInterpolator( new ValueFormatterMessageInterpolator() ).buildValidatorFactory();
Using the message parameter validatedValue and an optional format string you can interpolate the validated value. As example take a custom @Future constraint message - "${validatedValue:%1$td.%1$tm.%1$ty} is not in the future", which would interpolate into something like "13.01.11 is not in the future".

One note regarding backwards compatibility. Due to BVTCK-12 resp. HV-395 the javax.validation.Path implementation of Hibernate Validator needed some changes. The good news is, however, that unless you iterate over the Path instance returned by Constraint.getPropertyPath() you are not affected by this change.

Also the programmatic configuration of generic constraints has changes and looks like this now:

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Foo.class )
    .property( "bar", FIELD )
    .genericConstraint( MyConstraint.class ).param( "value", 1 );
We also added a new type parameter to ConstraintDef which simplifies the creation of definition types for custom constraints. It is now not longer necessary to provide methods for standard constraint attributes such as message(). If you are using a custom ConstraintDef you will have to retrofit your implementation.

Of course this release contains also a whole range of other bug fixes and improvements. You can view the full changelog here. Thanks to Kevin who helped to fix many of these issues. To point out one of his contributions have a look at HV-307 which adds support for Joda Time types such as DateTime. These types can be annotated with @Past and @Future just as the standard JDK date types.

We are looking forward to your feedback on the Validator Forum. For bug reports please use the issue tracker.

12 comments:
 
13. Jan 2011, 15:30 CET | Link

Very nice! Thanks Serli via Kevin for their contribution. Actually it looks like Hardy did nothing but integrate contributions ;)

ReplyQuote
 
13. Jan 2011, 21:41 CET | Link
Federico Mancini

Nice to see that the boolean composition made it in the new release :) Thanks again for the support!

 
13. Jan 2011, 21:46 CET | Link

Congratulations on the release. Is it just a drop-in-jar-replacement on AS 6 Final?

 
14. Jan 2011, 09:45 CET | Link
Nicklas Karlsson wrote on Jan 13, 2011 15:46:
Congratulations on the release. Is it just a drop-in-jar-replacement on AS 6 Final?

Yes, Only if you make extensive use of the Path API or if use the programmatic constraints configuration you might have to make some adjustments.

 
14. Jan 2011, 11:30 CET | Link
Hardy Ferentschik wrote on Jan 14, 2011 03:45:
Nicklas Karlsson wrote on Jan 13, 2011 15:46:
Congratulations on the release. Is it just a drop-in-jar-replacement on AS 6 Final?
Yes, Only if you make extensive use of the Path API or if use the programmatic constraints configuration you might have to make some adjustments.

And you don't drop in this baby, you gently put it in place. Be human!

 
14. Jan 2011, 13:00 CET | Link
Federico Mancini wrote on Jan 13, 2011 15:41:
Nice to see that the boolean composition made it in the new release :) Thanks again for the support!

We have to thank you guys for your contribution :)

 
16. Jan 2011, 22:48 CET | Link

Those interested in the method validation feature can find some more detail information in my blog post Method Validation with Hibernate Validator 4.2.

If you want to try method validation out yourself you might be interested in the methodvalidation-integration project I started on GitHub which provides validation interceptors for CDI and Google Guice.

As Hardy wrote we are happy about any feedback on the Beta, so just give it a try.

 
18. Jan 2011, 11:05 CET | Link
Gunnar wrote on Jan 16, 2011 16:48:
Those interested in the method validation feature can find some more detail information in my blog post Method Validation with Hibernate Validator 4.2.

Nice blog. I added the link as well to the wiki page Hibernate Validator on the Web. It already contains several links to articles and blogs about Validator.

 
21. Jan 2011, 14:05 CET | Link

BTW, has there been any discussion regarding hooks for client-side validation integration with JSR-303 in some MR?

10. Jun 2014, 12:58 CET | Link

Actual product packaging and materials may contain more and different information than what is shown on our website. We recommend that you do not rely solely on the information presented and that you always read labels, warnings, and directions before using or consuming a product. Please see our full disclaimer below. check out here

13. Jun 2014, 08:43 CET | Link

We also offer an in-house licensed esthetician services. We offer plastic surgery in Medford Oregon and it's surrounding areas.website link

03. Jul 2014, 18:53 CET | Link

This site is a leading resource for information about the James Bauer What Men Secretly Want PDF book that teaches you how to keep a guy interested and exactly what he needs in your relationship if he's going to stick around.what do all men secretly want

Post Comment