We have a use case in which we have a fairly poorly constructed bean that contains fields like so:
public class DataBean {
private boolean flag1;
private boolean flag2;
private String phone1;
private String address1;
private String city1;
private String state1;
private String phone2;
private String address2;
private String city2;
private String state2;
}
We need to validate phone/address/city/state [1|2] only if flag [1|2] is true. Bad, bad design, granted.
Our current strategy is to use @NotNull (or whatever validations we need) on each of the "real" data fields, and use a groups indicator, like so:
public class DataBean {
private boolean flag1;
private boolean flag2;
@NotNull(groups = Info.First.class)
private String phone1;
@NotNull(groups = Info.First.class)
private String address1;
@NotNull(groups = Info.First.class)
private String city1;
@NotNull(groups = Info.First.class)
private String state1;
@NotNull(groups = Info.Second.class)
private String phone2;
@NotNull(groups = Info.Second.class)
private String address2;
@NotNull(groups = Info.Second.class)
private String city2;
@NotNull(groups = Info.Second.class)
private String state2;
}
In our business logic where we are validating this bean (which has various other fields in it that will be validated by the "default" validation group), we will get violations for the "default" group, then check if flag1 is true, if so, run validations for Info.First.class, check if flag2 is true, then run validations for Info.Second.class.
Now the question ... is there any way of hooking into these groups from a custom class validator? I'm envisioning having a class validator that takes the flag1/flag2 properties and their corresponding custom groups and when isValid is invoked, it does these secondary/tertiary calls for the groups. The purpose being, simply, that the custom class validator would be in the default group, so the business logic validating this class wouldn't have the details of this ugly legacy design leak into it by virtue of having to call the validation separately.
Thoughts? Thanks!