InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

JSR 303: Time to Standardize Validation

Posted by Floyd Marinescu on Jul 12, 2006

Sections
Development
Topics
JCP Standards ,
Java
Tags
JSR 303 ,
Validation
Jason Carreira (who wrote the XWork validation framework) has submitted JSR 303 to standardize a meta-model and and API for JavaBean validation. The JSR has just been submitted and has not yet been approved by the JCP EC, but if approved the API could provide a consistent validation approach across tiers and within other standards and frameworks.  InfoQ spok to Jason to find out more about the potential of JSR 303.

According to Jason, there is a need for this standard because: 
Validation is done now in an ad-hoc fashion across all of the application tiers, and usually ends up being duplicated. There are some frameworks, like commons-validator and the XWork validation framework, which look to provide a reusable framework for validation, but very few organizations make full use of validation across the view and model tiers, and there's a lot of duplication and wasted effort in this area. There's also a need to plug in validation in several other JSR's, for example JSF and JPA. In order for these JSRs to not duplicate validation efforts, there needs to be a validation JSR for them to reuse since they can't be based on using an opensource library.

On the types of validation that will be specified in the metamodel: 
I think more than anything it's important to define the framework for applying validations and defining validators. The common validations will be included, such as "required", date and number ranges, etc. Depending on how the Expression Language JSR is progressing, we may be able to have an EL validator that gives a more ad-hoc validation mechanism. I'm also hoping to keep the ability to pass validation along to a sub-property to apply that objects validations, like we do with the XWork validation framework with the Visitor field validator.
The Visitor field validator really enables the modular reuse of domain model validations across different validation contexts.

I think the most important thing, though, will be to define the ability to build your own custom validations using both annotations and XML meta-data. Obviously the limitations of annotations provide interesting challenges here, but we've got some time to work that out.
On how how the validation facility could be used consistently across different tiers:
Well, I'll have to use the XWork validation framework as the example.  I built the XWork validation framework while we were facing issues of implementing consistent validations across both the view tier and the domain model for batch processing at Notiva. At my current employer, ePlus, we apply our validations to our Hibernate mapped domain model classes and use the validations in our web UI and also in our batch import processing. Mostly these validations are the same, but there's also some validations which are UI specific, for example entering the password twice when adding a new user. The concept of validation context specific validations in the XWork validation framework allow us to have a common set of validations which are always applied, and also have additional validations which are specific to whether the validation is done in a web UI context or a batch processing context.  It's all a common set of validation meta-data, but the context-sensitive ability lets us customize the validations to the use case.

Jay Bose and Matt Raible also blogged approval of the initiative, with Matt adding "In an ideal world, the RIFE, Spring MVC, Stripes, Struts, Tapestry and Wicket developers will all participate and allow JSR-303's result to be used as their framework's validation engine. I think it's a given that this will be usable with JSF."
Not just for webapp frameworks by Jason Carreira Posted
Great idea by Alexandre Poitras Posted
Cool by Corby Page Posted
Re: Cool by Jason Carreira Posted
Re: Cool by Corby Page Posted
Script-based validation? by Charles Nutter Posted
struts 1 and jsr 303 integration with sbva by A A Posted
  1. Back to top

    Not just for webapp frameworks

    by Jason Carreira

    While I agree with Matt that I'd love to have people from those web frameworks involved, it's also important to point out that this is not just a web validation spec. I joined the JSR-295 Bean Binding expert group to make sure JSR-303 will align with it. Bean Binding started as a Swing UI idea for binding UI form fields to bean properties, but is now a JavaBean spec that's not platform dependent. Likewise, my background is in web application development, but bean validation is just as important in a rich client Swing UI application as it is in a web application, so we're definitely going to support both by being an extension to the JavaBean contract for POJOs.

  2. Back to top

    Great idea

    by Alexandre Poitras

    Wow finally a down to earth JSR that doesn't push a new component model or try to save the world. Great initiative! Look forward for the results.

  3. Back to top

    Cool

    by Corby Page

    I vote yes on this baby, but I'd like to put in an early spec request.

    When I need to do custom validations, I frequently find that my logic is too complex to be expressed in ExpressionValidator. I hope you will add support for a validator that allows you to delegate to a custom boolean method within the class.

    Example:

    @CustomValidator(message = "Complex validation failed", shortCircuit = true, method = "complexValidator" )

  4. Back to top

    Re: Cool

    by Jason Carreira

    You should be able to do that now with the expression validator... just make the expression "complexValidator()".

  5. Back to top

    Re: Cool

    by Corby Page

    Thanks!

  6. Back to top

    Script-based validation?

    by Charles Nutter

    As much as folks seem to gush over XML, it seems like there may be a potential for scripted validation to be easier to manage and work with. Specifying validation rules in a Javascript or Ruby would be easy to read, and with the Java 6 release we've got Javascript out-of-the-box. Does it make sense to now start considering using scripting languages for roles like validation where we've been mostly declarative in the past?

  7. Back to top

    struts 1 and jsr 303 integration with sbva

    by A A

    I have written a struts 1 jsr-303 integration library available at

    code.google.com/p/sbva/

    to allow easy integration of jsr-303 with struts 1. If you try it
    please let me know what you think.

Educational Content

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.

Beauty Is in the Eye of the Beholder

Alex Papadimoulis discusses ugly code, where it comes from, how to avoid it, and how to get rid of it.

Architecting Visa for Massive Scale and Continuous Innovation

John Davies examines Visa’s architecture and shows how enterprises have architected complex integrations incorporating Hadoop, memcached, Ruby on Rails, and others to deliver innovative solutions.

Max Protect: Scalability and Caching at ESPN.com

Sean Comerford unveils ESPN.com’s architecture, what components are used and why, and the current changes the website goes through.

The Seven Deadly Sins of Enterprise Agile Adoption

Are there repeated patterns of failure on Enterprise Agile Enablement efforts? Sanjiv and Arlen discuss Seven Deadly Sins to avoid when adopting Agile in an enterprise.