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.

Initial Draft of the Bean Validation Specification Released

Posted by Charles Humble on Apr 09, 2008

Sections
Development
Topics
JCP Standards ,
Java
Tags
Validation
In most enterprise applications data constraints will exist in two places:
1. In the model (typically as database schema constraints).
2. In the application code.
Both are important. Database constraints allow reuse of the underlying data model in the event that the application code needs to be migrated. Application level validation is able to provide finer grained control (is this a valid email address? is the customer's date of birth in the future?) than can be easily achieved with model level constraints, and can also more readily provide meaningful error messages to the users of the application. Application level validation may in itself reside in multiple places, resulting in a large amount of duplication of effort across the different application tiers. In a typical web application, for example, JavaScript is executed in the browser for simple field level validations and the serverside layer is used for more complex business rule validations. It would be highly desirable to be able to centralize the validation definitions in one place and share the definitions across the different layers.

Led by Hibernate Validator lead developer Emmanuel Bernard, JSR-303 aims to standardize the constraints metadata model for Java EE 6. An early draft of the specification has been released and the expert group are keen to solicit feedback. As part of this a forum has been set up, and Bernard has begun to publish a series of articles (part 1, part 2) on the Hibernate blog describing how the API works.

Unsurprisingly given its parentage, JSR-303 is heavily influenced by the JBoss' Hibernate Validations although a number of the other validator frameworks (Xwork and the Apache Commons Validator for instance) have also influenced the specification. It uses annotations for the majority of cases and provides standard APIs for runtime validation and to query the metadata. Each constraint annotation must define a message of type String which is used to create an error message. Error messages support internationalisation. Constraints may be declared on an object's fields, getters, class, superclass and interfaces and validating an object will validate all of its constraints. For example the following code creates a String called street1 which has a maximum length of fifty characters and is mandatory:

@NotEmpty @Max(50)
private String street1;

The framework is designed to be extensible so that an application can easily define its own additional set of constraints. From the first blog entry:

"A constraint is comprised of:
• an annotation
• a constraint validation implementation
While the annotation expresses the constraint on the domain model, the validation implementation decides whether a given value passes the constraint or not."

As well as supporting instance validation the specification supports validation of an object graph, so for example if a bean ClientDetails contains an Address bean with one or more @Valid annotations the validator will validate the contents of the Address bean when the ClientDetails bean is validated.

One significant difference between the spec and the Hibernate Validator is the concept of groups, which provide a means for creating subsets of validations. Groups have an associated sequence (set via an @GroupSequence annotation) so that a developer can force one set of constraints to pass without error before the next set executes. Groups also allow a partial validation of the JavaBean. The draft specification suggests two scenarios where this might be useful:

"• the second group depends on a stable state to run properly
• the second group is a heavy consumer of time, CPU or memory and should be avoided if possible"
A number of different pieces of technology in the Java EE 6 platform should be able to take advantage of JSR-303. For example DDL updates when generated via an ORM tool, entity validation on insertion/update by the Java Persistence API, the new WebBeans API, and JavaServerFaces components all look to be obvious candidates.
Blog entry on groups by Emmanuel Bernard Posted
XML Forms support? by Martin Probst Posted
Re: XML Forms support? by Emmanuel Bernard Posted
  1. Back to top

    Blog entry on groups

    by Emmanuel Bernard

    I have just written a blog entry on groups here.


    Emmanuel Bernard

  2. Back to top

    XML Forms support?

    by Martin Probst

    It would be highly interesting if there was an implementation that could automatically map from JSR-303 metadata to other validation schemes, e.g. XML Forms types, client side JavaScript etc.

    Anything like that?

  3. Back to top

    Re: XML Forms support?

    by Emmanuel Bernard

    The spec is written in such a way and exposes metadata APIs so that your client side JS library, your XML Forms type consumer and so on can go and extract the info from the domain model. The next step is to talk to the JPA 2 and JSF 2 expert groups to match this integration in the respective specifications.
    Specific converters are not part of the spec because as you pointed out there are gazillions of validation schemes :)

Educational Content

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

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.