Help

Hibernate Validator 4.2.0.CR1 is finally ready for download via the JBoss Maven Repository or SourceForge. We promise you won't have to wait so long for 4.2.0.Final. In total we addressed 28 issues. Most of the issues where minor bug fixes, documentation and code refactorings. Thanks you everyone providing bug reports and helping us to make Validator even better :-)

The biggest change is the ability to now also configure method level validation programmatically. To implement this in an unambiguous way we had to make yet some more changes to the programmatic API. Remember how the programmatic API looked like in Beta2? Here is an example:

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Car.class )
    .property( "manufacturer", FIELD )
        .constraint( NotNullDef.class )
    .property( "licensePlate", FIELD )
        .constraint( NotNullDef.class )
        .constraint( SizeDef.class )
            .min( 2 )
            .max( 14 )
    .property( "seatCount", FIELD )
        .constraint( MinDef.class )
            .value ( 2 )
.type( RentalCar.class )
    .property( "rentalStation", METHOD)
        .constraint( NotNullDef.class ); 

With the CR1 API the same example looks like:

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Car.class )
    .property( "manufacturer", FIELD )
        .constraint( new NotNullDef() )
    .property( "licensePlate", FIELD )
        .constraint( new NotNullDef() )
        .constraint( new SizeDef()
            .min( 2 )
            .max( 14 ) )
    .property( "seatCount", FIELD )
        .constraint( new MinDef()
            .value ( 2 ) )
.type( RentalCar.class )
    .property( "rentalStation", METHOD )
        .constraint( new NotNullDef() ); 

As you can see the difference is that you now have to instantiate the definition classes. That's all. Not too bad, right? Programmatic method level validation looks like this:

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Car.class )
    .method( "drive", String.class, Integer.class )
        .parameter( 0 )
            .constraint( new NotNullDef() )
            .constraint( new MinDef().value ( 1 ) )
        .parameter( 1 )
            .constraint( new NotNullDef() )
        .returnValue()
            .constraint( new NotNullDef() )
    .method( "check" )
        .returnValue()
            .constraint( new NotNullDef() );   

You find the full change log for this release here. Please provide feedback via the Validator Forum and the Jira issue tracker.

Thanks to Gunnar and Kevin to once more were driving most of the work.

Enjoy!

P.S. If you have any opinion what should go into Bean Validation 1.1 let us know!

5 comments:
 
10. Jun 2011, 08:40 CET | Link

Cool. What was the reason for the change BTW?

 
10. Jun 2011, 09:27 CET | Link

And why not using the Meatmodel features brought by JPA 2.0 ? You could then have a completely type-safe API. Something like

ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Car.class )
    .property( CAR_.manufacturer)
        .constraint( new NotNullDef() )
    .property( Car_.licensePlate)
        .constraint( new NotNullDef() )
        .constraint( new SizeDef()
            .min( 2 )
            .max( 14 ) )
    .property( Car_.seatCount)
        .constraint( new MinDef()
            .value ( 2 ) )
.type( RentalCar.class )
    .property( RentalCar_.rentalStation)
        .constraint( new NotNullDef() ); 

Maybe it would make sense to take the Metamodel features out of JPA 2.1 and make it a seperate spec that could be used by JPA and Bean Validation (and maybe other use cases)

 
10. Jun 2011, 11:39 CET | Link
Emmanuel Bernard wrote on Jun 10, 2011 02:40:
Cool. What was the reason for the change BTW?

Without the change we had some ambiguities in the API, because we could not inject context into the ConstraintDef classes. See Gunnar's summary of the issue

 
10. Jun 2011, 11:44 CET | Link
Antonio Goncalves wrote on Jun 10, 2011 03:27:
And why not using the Meatmodel features brought by JPA 2.0 ? You could then have a completely type-safe API.

Absolutely. We discussed this and we might offer this option as well, but for the first cut of the API we did not want to enforce the use of a annotation processor to built the metamodel. Even JPA 2 offers both alternatives. It will be also interesting whether the programmatic API will become part of the BV 1.1 spec and if so in which form.

 
11. Jun 2011, 00:52 CET | Link
And why not using the Meatmodel features brought by JPA 2.0 ?

Using the meta-model is definitely a good idea and I think we might support this later on. IMO this would have to happen in some kind of optional manner though, HV shouldn't have a dependency on JPA.

Maybe it would make sense to take the Metamodel features out of JPA 2.1 and make it a seperate spec that could be used by JPA and Bean Validation (and maybe other use cases)

I'd love to see the meta-model feature becoming independent from JPA entities and becoming available for any Java bean. There is a need for specifying properties in a safe way also in many other areas (data binding, collection processing etc.).