Help

With the release of Hibernate Validator 5.1.0.Alpha1 just out, it is time to pick up on the Bean Validation 1.1 spotlight series and cast a light onto method validation. Long time Hibernate Validator users know that method validation is part of Validator since version 4.2, however only as a provider specific featue. Bean Validation 1.1 makes method validation now part of the specification.

But what is method validation? Method validation is the ability to place constraint annotations onto methods or constructors and/or their parameters. Here is an example:

public class User {
     public User(@NotNull @Size(max = 40) String firstName, @NotNull @Size(max = 40) String lastName) {
        // ...
     }

     @NotNull
     @Valid
     public Order placeOrder(@NotNull @Valid Item item) {
          // ...
     }

     @PasswordsMatch
     public void resetPassword( @NotNull String password, @NotNull String passwordConfirmation) {
          // ...
     }

     // ...
}

The constraints describe that whenever the constructor of User is called, firstName and lastName cannot be null and neither can exceed 40 characters. Also, whenever the user places an order, the provided Item instance cannot be null and must be valid. Valid in this context means that the instance itself passes the validation of all its property (constraints placed on fields and getters) and class level constraints. The returned Order instance of the placeOrder() call is a non null instance which also passes all bean constrains for this type. Last but not least, if the user resets his password, the new password and the password confirmation cannot be null and they have to match. The latter is expressed via a so called cross parameter constraint - @PasswordMatch.

Overall, the shown constraints define the pre- and postconditions for a given method or constructor call. This is commonly known as programming by contract which allows to make the specification of the these constraints part of the method. It also avoids code duplication, since these checks don't have to be implemented redundantly in the body of the methods.

But how do these constraints get validated then? Bean Validation offers for this purpose ExecutableValidator which can be retrieved from a Validator instance via Validator.forExecutables(). This ExecutableValidator offers the methods to validate method/constructor parameters and return values. This in itself, however, is not very helpful for an application developer. She would have to write the required code to handle the validation herself which is not a trivial task. Instead the intention is that frameworks and libraries provide the integration for the application developer via some sort of AOP, interceptor or proxy based approach. That way, method validation occurs transparently upon invocation of constrained methods or constructors, throwing a ConstraintViolationException whenever one more more constraints are violated. An example of this type of integration is the Java EE 7 framework.

In Java EE 7 all CDI managed beans are automatically method validated. All you have to do is to place constraints on methods or constructors and their parameters. CDI will then, via its interceptor capabilities, evaluate all method validation constraints. In this context it is important to know that Bean Validation does not validate getters (method name starts with get, has a return type and no parameters or starts with is, is returning boolean and has no parameters) per default. This is to avoid conflicts with the validation of bean constraints. This behaviour can be configured via the use of @ValidateOnExecution or in validation.xml using default-validated-executable-types. The specification contains all the details and also gives some examples.

Last but not least, another caveat. When using method validation constraints within class hierarchies, the specification demands that the Liskov substitution principle holds. Formally this means that a method's preconditions (as represented by parameter constraints) must not be strengthened and its postconditions (as represented by return value constraints) must not be weakened in sub-types. Concretely it implies that in sub-types no parameter constraints can be declared on overridden or implemented methods. Also parameters cannot be marked for cascaded validation. Instead, all parameter constraints must be defined at the method's root within the hierarchy. The Bean Validation implementation will throw a ConstraintDeclarationException if the Liskov substitution principle is violated. The complete set of rules can be found in Method constraints in inheritance hierarchies of the specification.

I hope this blog helped to shine some light on the biggest new feature of Bean Validation 1.1 - method validation. If you have questions, leave a comment, contact us via the forum or chat to us in IRC.

Happy validating!

P.S. We will talk about the changes to the metadata and javax.validation.Path API related to method validation in another post. We also will have a closer look at the details of implementing cross parameter constraints. Stay tuned.

30 comments:
 
21. Nov 2014, 04:37 CET | Link

could be a means associated with communication where one really listens towards the lyrics as well as understands what's being said within the song or simply an accumulation of sounds come up with to create a rhythmcrystal x asli

 
22. Nov 2014, 12:18 CET | Link

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck. home warranty reviews home warranty reviews home warranty reviews home warranty reviews

 
25. Nov 2014, 08:08 CET | Link

The world is changing fast. people are also being changed.day by day we are becoming more dependant on degital system.yoU make me think of this really.You have a nice way of sharing your thoughts. personal trainer berlin

 
25. Nov 2014, 08:40 CET | Link

Great points. I was just talking with a reader who suggested I needed to make more obvious what the purpose of my site is.CBSE

 
26. Nov 2014, 08:49 CET | Link

Thank you so much for the post you do. I like your post and all you share with us is up to date and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. cortinas luxalfex

 
26. Nov 2014, 10:55 CET | Link

You could post on the friendless of the blog owner. You may illustrate it's mesmerizing. Your blog feedback may add to your contacts. Alfonso Losa

 
26. Nov 2014, 12:46 CET | Link
custom cabinet | webseobuy67(AT)yahoo.com

I agree with you. This post is truly inspiring. I like your post and everything you share with us is current and very informative, I want to bookmark the page so I can return here from you that you have done a fantastic job. kitchen cabinet doors unfinished kitchen cabinets unfinished cabinets kitchen cabinet custom cabinet

 
27. Nov 2014, 12:00 CET | Link

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, дизайн студия интерьера дизайн интерьера Москва

 
02. Dec 2014, 11:54 CET | Link
follow instructions here

It is which problems most and results in improving the guests to your web page and thereby resulting in into a number of customers and customers, again which gradually provides more earnings for you. follow instructions here

 
02. Dec 2014, 11:59 CET | Link

As you might expect, this is a question that carries significant weight as there are millions of people who have this condition, which is not only irritating (itchiness, dryness) but can be cause for embarrassment. Compounding this is the fact that there are several different types of eczema, which can make it difficult to ascertain the cause of the irritation. http://cocfreegems.net/

 
02. Dec 2014, 12:45 CET | Link
click

SEO is also very inexpensive and easy to use. However, choosing the best SEO Organization to meet your organization's particular needs can be challenging. click

04. Dec 2014, 09:23 CET | Link

I am grateful to you and expect more number of posts like these. Thank you very much.

04. Dec 2014, 12:30 CET | Link

Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic.

04. Dec 2014, 15:05 CET | Link

Awesome article! I want people to know just how good this information is in your article.

05. Dec 2014, 16:33 CET | Link

I just want to let you know that I just check out your site and I find it very interesting and informative.

06. Dec 2014, 08:27 CET | Link

Great stuff.You have done the best ever job by creating such a fantastic site.Here is another great site roblox free robux which will let you to generate the roblox free robux at zero penny.

 
06. Dec 2014, 09:10 CET | Link

I am just pretty happy to sign up ones own guestbook these days,Your own feedback and listed below are excellent in my opinion,incidentally I should say also definitely will teach some great supplements for your needs every. first american home warranty first american home warranty first american home warranty first american home warranty first american home warranty

06. Dec 2014, 18:05 CET | Link

This is such a great resource that you are providing and you give it away for free.

10. Dec 2014, 09:49 CET | Link

Best work you have done, this online website is really cool with great facts.

 
12. Dec 2014, 12:29 CET | Link
jack

Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work http://catlittercoupons.org/

 
12. Dec 2014, 13:28 CET | Link
jack

This is an awesome post. Just one of the very best post I've ever seen. What a really good and awesome post. Keep up your work on articles autooilchangecoupons.com

 
13. Dec 2014, 08:51 CET | Link

As you might expect this is a question that carries significant weight as there are millions of people who have this condition which is not only irritating itchiness dryness but can be cause for embarrassment. Compounding this is the fact that there are several different types of eczema which can mae it difficult to ascertain the cause of the irritation. http://goodstone.com/

 
13. Dec 2014, 08:52 CET | Link
ayrt wrote on Dec 13, 2014 02:51:
As you might expect this is a question that carries significant weight as there are millions of people who have this condition which is not only irritating itchiness dryness but can be cause for embarrassment. Compounding this is the fact that there are several different types of eczema which can mae it difficult to ascertain the cause of the irritation. http://goodstone.com/

Click HELP for text formatting instructions. Then edit this text and check the preview.

13. Dec 2014, 17:42 CET | Link

I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.

16. Dec 2014, 11:12 CET | Link

This article was written by a real thinking writer. I agree many of the with the solid points made by the writer. I’ll be back.

16. Dec 2014, 15:33 CET | Link

This is a great inspiring article.I am pretty much pleased with your good work.

 
16. Dec 2014, 23:22 CET | Link
alba

I am certainly thankful to you for providing us with this invaluable info. My spouse and I are truthfully grateful, precisely the computer data we needed

creditos online

 
17. Dec 2014, 08:57 CET | Link

Another type of meals that has been connected to acne is gluten, or quite simply, meals such as breads, rice, cereal items and oatmeal. Something else to consider is that acne can also be connected to beverages that contain gluten, such as main alcohol and whole rice alcohol alcohol. For those that have issues with gluten centered meals, there is the regular gas but this can also cause to stomach issues as well as acne. raccoon removal ny