Changing default group in custom constraint annotations

Viewed 236

I have a working custom class level constraint that validates all annotations that do not belong in a group. How can I change that to validate a specific group with that constraint?

This is the annotation class I have:

@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = CheckCase.class)
@Documented
public @interface ValidName {

    String message() default "{message}";

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

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

    @Target({TYPE, ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Documented
    @interface List {
        ValidName[] value();
    }
}

Right now, the validation works only if I use:

@CheckCase(message = "Error message")

I would like to use this:

@CheckCase(message = "Error message", groups = CheckCaseGroup.class)

Do I have to change something in the following line?

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

Separate question: If I run the validator without passing in a group, will it go through all validations including the ones that have a group, or will it go only through the ones that don't have a specified group? For example:

will running this:

validator.validate(inputData);

go through this annotations:

@NotBlank(groups = CheckCaseGroup.class)

or only this:

@NotBlank()
0 Answers
Related