If you want to check POJO, you can take a look at the Oval structure http://oval.sourceforge.net/
JSR-303 (and its implementation) can also help if ur application is developed based on pojos or beans
Here is an example of a custom check in OVal: Let's say you need to check the variable map in SomeValueClass and make sure that the welcome key is always present.
public class SomeValueClass { @FormCollection Map variables; public static void main(String[] args) { SomeValueClass svc1 = new SomeValueClass(); svc1.variables = new HashMap(); svc1.variables.put("greeting", ""); Validator validator = new Validator(); List<ConstraintViolation> violations = validator.validate(svc1); System.out.println("svc1 violations.size() = " + violations.size()); }
@FormCollection annotations on "Variable maps"; is a regular validator and looks like this:
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) @net.sf.oval.configuration.annotation.Constraint(checkWith = FormCollectionCheck.class) public @interface FormCollection { String message() default "Some errors in the form"; }
And the constraint check class will look like this:
public class FormCollectionCheck extends AbstractAnnotationCheck<FormCollection> { public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator) { Map vars = (Map) valueToValidate; return !(vars.get("greeting")==null || ((String)vars.get("greeting")).length()<=0); } }
Hope this helps, Greetings
source share