(Disclosure: I'm the author)
Saripaar v2 allows you to define custom annotations.
Here's how you do it.
Step 1 Define your custom annotation as follows. Make sure you have a RUNTIME
retention policy and your annotation must be targeted towards FIELD
element types. The message
and messageResId
attributes are mandatory, so watch the names and the types.
@ValidateUsing(HaggleRule.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Haggle {
public int messageResId() default -1; // Mandatory attribute
public String message() default "Oops... too pricey"; // Mandatory attribute
public int sequence() default -1; // Mandatory attribute
public double maximumAskingPrice(); // Your attributes
}
Step 2 Define your rule by extending the AnnotationRule
class.
public class HaggleRule extends AnnotationRule<Haggle, Double> {
protected HaggleRule(Haggle haggle) {
super(haggle);
}
@Override
public boolean isValid(Double data) {
boolean isValid = false;
double maximumAskingPrice = mRuleAnnotation.maximumAskingPrice();
// Do some clever validation....
return isValid;
}
}
Step 3 Register your rule.
Validator.registerAnnotation(Haggle.class); // Your annotation class instance
Simple as that. Take a look at the source code if you want to. Saripaar v2 is now available on Maven Central.