简体   繁体   中英

JSR-303 Bean Validation - Custom Constraint Multiple Annotations to One Validator

When writing custom constraints is it possible to have multiple annotations validated by one validator implementation. For example I have several annotations which stipulate different @size annotations but I want them all to point at the same validator class which does some global checking ie all must be match a certain regex. As far as I can see the implementation takes in one Annotation Type.

One annotation

@Target( { METHOD, FIELD, ANNOTATION_TYPE, TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {UCNValidator.class})
@Documented
@Size(min = 9, max = 9, message = "{exactlength}")
public @interface UCN {

    String message() default "{invalidFormat}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    String fieldName() default "ucn";

}

The validator

public class UCNValidator implements ConstraintValidator<UCN, String>
{

    private String pattern = "[a-zA-Z].*";
    private String fieldName;

    @Override
    public void initialize( UCN constraintAnnotation )
    {
        this.fieldName = constraintAnnotation.fieldName();
    }

    @Override
    public boolean isValid( String value, ConstraintValidatorContext constraintValidatorContext )
    {

        if ( value != null )
        {
            if ( !value.matches(pattern) )
            {
                //do some stuff
                return false;
            }

        }

        return true;

    }

There doesn't seem to be a way to access other values from an object when validating one of its properties. The solution I use is to put the annotation on the class, then the validator will get the entire object in to validate against, and you can access just the info you need to perform the validation.

Here's one I wrote to compare two different properties of an object against each other:

@Target(TYPE)
@Retention(RUNTIME)
@Constraint(validatedBy = LessThanValidator.class)
@Documented
public @interface LessThan {

    String message() default "{com.bullethq.constraints.LessThan}";

    String bigValueProperty();

    String littleValueProperty();

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

Then the validator class is:

public class LessThanValidator implements ConstraintValidator<LessThan, Object> {

    private LessThan constraint;

    public void initialize(LessThan constraintAnnotation) {
        constraint = constraintAnnotation;
    }

    public boolean isValid(Object object, ConstraintValidatorContext cvc) {
        Object bigValue = getValue(object, constraint.bigValueProperty());
        Object littleValue = getValue(object, constraint.littleValueProperty());

        // If one of the values is null, then we do not perform validation.
        if (bigValue == null || littleValue == null) {
            return true;
        }

        if (bigValue instanceof Comparable && littleValue instanceof Comparable) {
            boolean valid = ((Comparable<Object>) bigValue).compareTo(littleValue) > 0;
            if (!valid) {
                // If the values are not valid, then build a custom violations which has the correct path in it.
                cvc.buildConstraintViolationWithTemplate(cvc.getDefaultConstraintMessageTemplate())
                        .addNode(constraint.littleValueProperty())
                        .addConstraintViolation().disableDefaultConstraintViolation();
            }
            return valid;
        }
        throw new IllegalArgumentException("Properties " + constraint.bigValueProperty() + " and " + constraint.littleValueProperty() + " both need to be comparable in " + object.getClass());
    }
}

The getValue() method is just a static method using reflection to get the value from the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM