Wednesday, March 31, 2010

Oval Validation Framework

I just gone thru the oval validation framewrok and found myself easy and best practice to use this for runtime validation check.

There are lot to explore but my basic tests,
I am personal feeling providing Constraint level check before calling the method is most advantage one..

My sample uses the custom annotation based constraints
For Example,
I want to call the method rank().
There are certain precondition,
Ex: If the subject marks are > 45 then only the rank can be found.
Instead of checking that using if else before calling the rank() or doing that inside rank() is not ok. But the Oval validation really helps in this case.


This is just an example based on Annotation.

Sample :

public void rank(@SubjectPass){
// Some logic goes hear
}

This is how the method definition. Before executing the logic inside the rank() the @SubjectPass will be executed. This one is Custom Annotation based on constraints.

How this works:
You should have some thing like this,

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Constraint(checkWith = SubjectPassCheck.class)
public @interface SubjectPass {

}

Then you should have the SubjectPassCheck class.

public class SubjectPassCheck extends AbstractAnnotationCheck
{
public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator)
{
// here your logic..
}
}


This is how the pre oval validation is happening.

There are so many things.
This article is really good. Please have look.

http://oval.sourceforge.net/userguide.html#d4e435

http://jee-bpel-soa.blogspot.com/2008/12/jsr-303-and-oval-validation-framework.html

2 comments:

  1. I just gone thru the oval validation framewrok and found myself easy and best practice to use this for runtime validation check.

    There are lot to explore but my basic tests,
    I am personal feeling providing Constraint level check before calling the method is most advantage one..

    My sample uses the custom annotation based constraints
    For Example,
    I want to call the method rank().
    There are certain precondition,
    Ex: If the subject marks are > 45 then only the rank can be found.
    Instead of checking that using if else before calling the rank() or doing that inside rank() is not ok. But the Oval validation really helps in this case.


    This is just an example based on Annotation.

    Sample :

    public void rank(@SubjectPass){
    // Some logic goes hear
    }

    This is how the method definition. Before executing the logic inside the rank() the @SubjectPass will be executed. This one is Custom Annotation based on constraints.

    How this works:
    You should have some thing like this,

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
    @Constraint(checkWith = SubjectPassCheck.class)
    public @interface SubjectPass {

    }

    Then you should have the SubjectPassCheck class.

    public class SubjectPassCheck extends AbstractAnnotationCheck
    {
    public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator)
    {
    // here your logic..
    }
    }


    This is how the pre oval validation is happening.

    There are so many things.
    This article is really good. Please have look.

    http://oval.sourceforge.net/userguide.html#d4e435

    http://jee-bpel-soa.blogspot.com/2008/12/jsr-303-and-oval-validation-framework.html

    ReplyDelete
  2. i didn't know that you have such a wonderful blog... keep writing...

    ReplyDelete