I have a DTO class where I made some fields mandatory. And, based on another type field where value can be assume A & B if type A, need to check only 1 item can be passed, if B it can be more than 1 also.
I need to check if a list has at least 1 value in DTO class level. And, in custom annotation validation I need to check size of list based on type field of DTO.
So, in DTO
@NotNull
@Size(min = 1)
private List<@NotBlank String> items;
And, inside annotation
if (cancelType == CancellationTypeEnum.A && cancelDto.getItems().size() > 1) {
isValid = false;
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(
"Only one item can be sent for 'A' cancel type.").addConstraintViolation();
}
But, since field level happens later, if I pass null for items field, it goes to this annotation validator & throw NPE as field is null & I am trying to get size
Temporary solution is I do null check, then check size, but in field level anyway we are giving @NotNull.
Is there any way to do class level custom annotation to validate after field level validation happens. In that case, it will throw field level validation as field is null & will not go to custom class level annotation
You could use JS-303 validation groups (see here), in conjunction with @Validated, from the documentation:
For example:
where
Primary
is a simple interface:and the same for
Secondary
:finally you could use validated as follows:
when using the above code with a
CancelDto
with items null, you should get: