2

I created my first custom validation annotation with validator class as inner class (which i find quite well-arranged). It looks like this:

@Target( { ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {OneOfGroupNotNull.Validator.class})
@Documented
public @interface OneOfGroupNotNull {

    // custom annotation properties
    String[] fields();

    // required by JSR-303
    String message() default "One of group must be not null. {fields}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

    public class Validator implements ConstraintValidator<OneOfGroupNotNull, Object> {

        private String[] fields;

        @Override
        public boolean isValid(Object bean, ConstraintValidatorContext cvc) {

            int countNotNull = 0;

            for (String field : fields) {
                try {
                    String property = BeanUtils.getProperty(bean, field);
                    if (property != null) {
                        countNotNull++;
                    }
                } catch (Exception ex) {
                    throw new RuntimeException("Validation for field " + field + " of type " + bean.getClass()+ " raised exception.", ex);
                }
            }

            return countNotNull == 1;
        }

        @Override
        public void initialize(OneOfGroupNotNull a) {
            fields = a.fields();
        }
    }
}

A bean class that is annotated with this validator may look like this:

@OneOfGroupNotNull(fields = {"a", "b", "c"})
public interface MyBean {
    String getA();
    Rarity getB();
    Boolean getC();
}

The problem is that I cannot find a way to format the string array "fields". It just takes the to string method which results in something like: One of group must be not null. [Ljava.lang.String;@157d954

2 Answers 2

2

If you changed type of fields from String[] to String then message with field names will be shown correctly. To get field names in constraint just split() it by comma.

Another option is to generating custom message inside constraint, like this:

cvc.disableDefaultConstraintViolation();
cvc.buildConstraintViolationWithTemplate("error message")
    .addNode("field name with error")
    .addConstraintViolation();
Sign up to request clarification or add additional context in comments.

Comments

0

Which implementation of Bean Validation are you using? If you are using Hibernate Validator 4.3 this should actually work. See also https://hibernate.onjira.com/browse/HV-506.

As a workaround, why not use a List? There the default toString is more sensible.

2 Comments

1. I am using Hibernate and it doesn't work out of the box. 2. A List is not a valid attribute for annotations.
Which version of Hibernate Validator are you using? We fixed this in 4.3, maybe you're using an onlder version? I just tried out your constraint and got the error message "One of group must be not null. [a, b, c]".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.