Saturday, May 9, 2009

OVal is an extensible general purpose validation framework for any kind of Java objects and provides you lots of basic validations that are required for any project.

You can even use OVAL to validate your entities. Now a days, JPA entities are just POJO beans, so using OVAL validation would save lots of your time and headache to write validators.

OVAL validation supports almost all the basic type of validations required for business purposes. The great part is, you can even extend the validations as per your requirement by using Java 5 annotations.

Check the link: http://oval.sourceforge.net/userguide.html

We have used OVAL in our product and it has saved lot of time of ours.

Consider a simple bean:


import net.sf.oval.constraint.Length;
import net.sf.oval.constraint.NotNull;

/**
* This class holds information about Person object.
*
* @author swapnil
*/
public class Person {
@NotNull
@Length(min = 2, max = 20)
private String name;

/**
* @param name - Name of the Person
*/
public void setName(String name) {
this.name = name;
}

/**
* @return String - Name of the Person
*/
public String getName() {
return this.name;
}


OVAL Usage:

/**
* This method is used to validate the Person object.
*/
public void validatePerson ()
{
// create the Validator instance
Validator validator = new Validator();

// Create object to validate
Person person = new Person();
// set invalid value
person.setName ("ThisisverylongnameforthePersonClass");

// ask the validator to validate
List violationsList = validator.validate (person);

if (violationsList.size () > 0)
{
for (ConstraintViolation violation : violationsList)
{
// print error messages
System.out.println (violation.getMessage ());
}
}
}

Summary

It surely is a powerful validation framework but some things are not working properly or are not intuitive.

Further tests of this validation framework are certainly required!