Saturday, April 4, 2009

Object validation by annotations with the OVal framework

OVal is a validation framework for any kind of java objects. For example when you want to validate a JavaBean you just have to annotate its properties like this :


public class object {
@NotNull
@NotEmpty
@Length(max=32)
private String name;
}

To process the validation just call the OVal validator :

net.sf.oval.Validator validator = new net.sf.oval.Validator();
List violations = validator.validate(obj);

if(!violations.isEmpty())
{
//Do whatever you want
}

In a real world application you need sometimes to return an error code to the client depending on the error type. Do not worry, OVal is doing it for you. Just add the error code property in your annotations like this

public class object {
@NotNull(errorCode="46")
...
private String name;
}

You can retrieve the error code in each ConstraintViolation object with the getErrorCode() method.

OVal does not only validate JavaBeans.

By utilizing AspectJ, OVal provides support for several aspects of programming by contract - however it is not a full blown programming by contract implementation.

With OVal you can

  • enforce that a parameterized constructor/method is invoked only if the given arguments satisfy prior defined constraints (precondition)

  • enforce that a method is invoked only if the object is in a certain state (precondition/invariant)

  • enforce that the return value of a method must satisfy prior defined constraints (postcondition)

  • enforce that the object must be in a certain state after a method has been executed (postcondition/invariant)


I suggest you read the user guide for more detailed examples.

Here is the list of the OVAL annotations :
Assert Check if evaluating the expression in the specified expression language returns true.
AssertConstraintSet Check if the value satisfies the all constraints of specified constraint set.
AssertFalse Check if the value is false.
AssertFieldConstraints Check if the value satisfies the constraints defined for the specified field.
AssertTrue Check if the value is true.
AssertURL Check if the value is a valid URL.
AssertValid Check if the value passes a validation by Validator.validate().
CheckWith Check the value by a method of the same class that takes the value as argument and returns true if valid and false if invalid.
CheckWithMultiple Check the value with the given CheckWith constraints.
DateRange Check if the date is within the a date range.
EqualToField Check if value equals the value of the referenced field.
Future Check if the date is in the future.
HasSubstring Check if the string contains a certain substring.
InstanceOf Check if the value is an instance of the specified class or implements all specified interfaces.
InstanceOfAny Check if the value is an instance of the specified class or implements one of the specified interfaces.
Length Check if the string representation has certain length.
MatchPattern Check if the specified regular expression pattern is matched.
Max Check if the number is smaller than or equal to X.
MaxLength Check if the string representation does not exceed the given length.
MaxSize Check if an array or collection does not exceed the given size.
MemberOf Check if the string representation is contained in the given string array.
Min Check if the number is greater than or equal to X.
MinLength Check if the string representation has at least the given length.
MinSize Check if the array or collection has at least the given number of elements.
NoSelfReference Check that the value is not a reference to the validated object itself.
NotBlank Check if the string representation is not empty and does not only contain white spaces.
NotEmpty Check if the string representation is not empty ("").
NotEqual Check if the string representation does not equal a given string.
NotEqualToField Check if value does not equal the value of the referenced field.
NotMemberOf Check if the string representation is not contained in the given string array.
NotNegative Check if the number is greater or equal zero.
NotNull Check if not null.
Past Check if the date is in the past.
Range Check if the number is in the given range.
Size Check if the array or collection has the given size.
ValidateWithMethod Check the value by a method of the same class that takes the value as argument and returns true if valid and false if invalid.


3 comments:

  1. You just copy and pasted the docs! This isn't helpful.

    ReplyDelete
  2. I agree, this is so very unhelpful

    ReplyDelete
  3. Thanks for your POST. One Question: is it possible to pass dynamic attribute value. For example: in the below line
    @NotNull(errorCode="46")

    Is it possible to pass the value of 46 reading from another JAVA variable

    ReplyDelete