6 Replies Latest reply on Nov 7, 2009 4:08 PM by pmuir

    Integration between Weld + JSR 303 Bean Validation?

    sboscarine

      Hello All,


      Does Weld integrate with JSR 303?

      Can I trigger validation in weld at arbitrary points to see if a bean validates according to my JSR303 annotations? 




      I have a prototype weld + JSF2 application up and running and wanted to introduce validation.  I added a @Size annotation It seemed to auto-magically recognize it in the JSF2 view tier (very impressive). 


      I have a hunch I've only scratched the surface of this new feature.  I couldn't find usage in the weld examples. 



      It seems 303 perfectly validates user input.  However, I was thinking about situations with complex logic.
        Can I arbitrarily check validations before persisting to ensure my business logic didn't modify the annotated bean and violate the constraints? 


      Another use case I was thinking about was for integration.  A decent part of my career has been transforming data from one format to another...such as a text file feed from a partner's mainframe into a JEE application.  It would be nice to adopt Weld for those jobs and have a convenient, easy to use, validation mechanism.


      Thanks,

      Steven

        • 1. Re: Integration between Weld + JSR 303 Bean Validation?
          gavin.king

          The only integration that we have in the spec is that you can inject the default ValidationFactory and Validator. (See section 3.6)


          If you want something beyond that, you need to write an interceptor.


          Note that JPA 2.0 also features transparent integration with 303.

          • 2. Re: Integration between Weld + JSR 303 Bean Validation?
            asookazian

            Because validation in web apps is essential, it would be nice if CDI/Weld supported the same validations that Seam 2.x supports: business validation via custom classes and Hibernate Validators with <s:validate>.


            How is the integration with other parts/specs of JEE 6?  i.e. JSF, JPA, web services, etc?  are custom interceptors required there as well?  I assume it's mostly DI-based...

            • 3. Re: Integration between Weld + JSR 303 Bean Validation?
              sboscarine

              Arbi Sookazian wrote on Nov 06, 2009 20:55:


              Because validation in web apps is essential, it would be nice if CDI/Weld supported the same validations that Seam 2.x supports: business validation via custom classes and Hibernate Validators with <s:validate>.



              Sounds like a JSF 2.0 request :)  Isn't <s:validate> a feature of the view tier? 


              Hopefully someone will write something that translates JSR 303 to JavaScript as well as server side validation in a convenient and easy-to-use fashion. 


              To me, server side validation is a failsafe.  Users hate server-side validation.  The more you can do in JavaScript, the happier your users will be. 


              • 4. Re: Integration between Weld + JSR 303 Bean Validation?
                asookazian


                To me, server side validation is a failsafe.  Users hate server-side validation.  The more you can do in JavaScript, the happier your users will be. 



                JS only works if the user has enabled it in their browser.  SS validation is less performant.  But Hibernate Validators and <s:validate> uses SS validation.  In fact, all validation is SS in JSF, no?

                • 5. Re: Integration between Weld + JSR 303 Bean Validation?
                  pmuir

                  Arbi Sookazian wrote on Nov 06, 2009 20:55:


                  Because validation in web apps is essential, it would be nice if CDI/Weld supported the same validations that Seam 2.x supports: business validation via custom classes and Hibernate Validators with <s:validate>.



                  This has been standardized by JSR-303 (the Hibernate Validator bit) and JSF2 (<f:validateBean />.



                  How is the integration with other parts/specs of JEE 6?  i.e. JSF, JPA, web services, etc?  are custom interceptors required there as well?  I assume it's mostly DI-based...


                  Take a look at the CDI spec, pretty much all the integration is specified there, but for your specific specs:



                  • Servlet integration supports all Servlet scopes

                  • JSF integration adds a conversation scope

                  • JPA integration allows you alias inject entity managers via @EntityManager

                  • WS integration via @Resource



                  None of these use or require interceptors.

                  • 6. Re: Integration between Weld + JSR 303 Bean Validation?
                    pmuir

                    Steven Boscarine wrote on Nov 06, 2009 21:34:



                    Arbi Sookazian wrote on Nov 06, 2009 20:55:


                    Because validation in web apps is essential, it would be nice if CDI/Weld supported the same validations that Seam 2.x supports: business validation via custom classes and Hibernate Validators with <s:validate>.



                    Sounds like a JSF 2.0 request :)  Isn't <s:validate> a feature of the view tier?



                    Yes, we took this largely wholesale from Seam, and made a few improvements along the way. Take a look at JSF 2 spec :-)




                    Hopefully someone will write something that translates JSR 303 to JavaScript as well as server side validation in a convenient and easy-to-use fashion. 

                    To me, server side validation is a failsafe.  Users hate server-side validation.  The more you can do in JavaScript, the happier your users will be. 



                    Well said :-)


                    We tried to get to a general solution to client side validation in JSF2, but ran out of time. However we did ensure that Bean Validation contains the necessary hooks for a third party to innovate - expect something from Seam 3 or RichFaces in this area.