Reputation: 6189
I have this model which i want to validate:
public class ClientDomain {
public interface AddValidations {
}
public interface UpdateValidations {
}
private String id;
@NotNull(groups = {ClientDomain.AddValidations.class})
@Size(min = 2, max = 10, groups = {ClientDomain.AddValidations.class})
private String name;
@NotNull(groups = {ClientDomain.UpdateValidations.class})
private ClientType type;
@NotNull(groups = {ClientDomain.AddValidations.class})
private Gender gender;
.....
}
Then, i am validating it like this:
Set<ConstraintViolation<Object>> violations = this.validator.validate(clientDomain);
This is not working, as far i know that is because all constraints validations belong to certains groups, then when i don't pass a group to validate, the validator takes by default the javax.validation.group.Default, then it validates nothing, i am right? if so, then is there any way to validate all constraints validations regardless the group they belong to? Regards!
PD: Obviusly, i do not want to do this:
Set<ConstraintViolation<Object>> violations = this.validator.validate(clientDomain, ClientDomain.AddValidations, ClientDomain.UpdateValidations);
Upvotes: 0
Views: 5767
Reputation: 330
What about use inheritance for it?
If ClientDomain
is your parent interface. You can do this:
>> when:
public interface ClientDomain extends Default {}
public interface AddValidation extends ClientDomain
public interface UpdateValidation extends ClientDomain
>> then
@NotNull(groups = {UpdateValidations.class})
private ClientType type;
@NotNull
private Gender gender;
In this example, Gender
is mandatory constraint for both groups. ClientType
on the other hand is only for UpdateValidation
. You will write less code and it is more clean to read also.
Btw. ClientDomain
is not necessary in this example. This is just for better imagination to show what you can do with this.
More details: hibernate validator docs
Upvotes: 4