I have a POJO annotated with JSR-303 annotations. Some of its attributes are other POJOs. I would like the inner POJOs to be @Valid, only if they are not null. But if they're null it's OK. Unfortunately I'm not succeding in doing this, so Java returns me the error for the inner POJOs attribute if they're null.
@AllArgsConstructor @NoArgsConstructor @Data
class OuterPojo{
@NotBlank private String attributeA;
@Valid private InnerPojo attributeB;
}
@AllArgsConstructor @NoArgsConstructor @Data
class InnerPojo{
@NotBlank private String attributeC;
@NotNull private Double attributeD;
}
I want an outerPojo to be valid if:
- attributeA is not-null and attributeB is null;
- attributeB is not-null and attributeB is not-null and valid.
So I would like the constraints on the attributes of inner pojo to be respected only if inner pojo is not null.
I've tried adding @Nullable to attributeB with no effect. How can I solve this?