Newbie question around hibernate validator. I have 2 classes, which have reference to another child class and depending on parent class validation on child class should change. Looking at documentation it seem like I should use SequenceProvider so I create sequence provider for each parent class and than groups for including appropriate validations on child class but it doesn't seem to work.
So here are my questions -
1) Is this correct approach? or should I use something else
2) What am I missing if this is right approach
UserCreate Class -Code:
@GroupSequenceProvider(CreateUserGroupSequenceProvider.class)
public class CreateUserRequest {
@Valid
@NotNull
protected IdentityRequest identity;
@Valid
protected RegistrationProfileRequest registrationProfile;
@Valid
protected ProfileRequest profile;
}
UserUpdate Class -Code:
@GroupSequenceProvider(UpdateUserGroupSequenceProvider.class)
public class UpdateUserRequest {
@Valid
protected IdentityRequest identity;
@Valid
protected RegistrationProfileRequest registrationProfile;
@Valid
protected ProfileRequest profile;
}
And here is the child class ex: IdentityRequest -Code:
public class IdentityRequest {
@NotNull(groups = CreateIdentityValidation.class, message = "Invalid email")
@Email(regexp = "(.+@[a-zA-Z0-9-.]+\\.[a-zA-Z0-9-.]+)", groups = {CreateIdentityValidation.class, EmailValidation.class}, message = "Invalid email")
@SafeHtml(whitelistType = NONE, groups = {CreateIdentityValidation.class, EmailValidation.class}, message = "Invalid email")
private String email;
@NotNull(groups = {CreateIdentityValidation.class, PasswordValidation.class}, message = "Invalid password")
@Size(min = 8, max = 255, groups = {CreateIdentityValidation.class, PasswordValidation.class}, message = "Invalid password")
private String password;
}
So I have created 2 sequence provider one for each Create and Update request. And than in IdentityRequest for each validation I have assigned groups.
For ex: When Parent = CreateRequest than email and password both are required whereas when UpdateRequest its optional.
CreateUserGroupSequenceProviderCode:
public class CreateUserGroupSequenceProvider implements DefaultGroupSequenceProvider<CreateUserRequest> {
@Override
public List<Class<?>> getValidationGroups(CreateUserRequest createUserRequest) {
List<Class<?>> defaultGroupSequence = new ArrayList();
defaultGroupSequence.add(createUserRequest.class);
if (createUserRequest != null) {
addIdentityValidationGroups(defaultGroupSequence, createUserRequest.getIdentity());
addRegistrationProfileValidationGroups(defaultGroupSequence, createUserRequest.getRegistrationProfile());
addProfileValidationGroups(defaultGroupSequence, createUserRequest.getProfile());
}
return defaultGroupSequence;
}
@Override
protected void addIdentityValidationGroups(List<Class<?>> defaultGroupSequence, IdentityRequest identityRequest) {
//validation group for username/password
defaultGroupSequence.add(CreateIdentityValidation.class);
}
}
UpdateUserGroupSequenceProviderCode:
public class UpdateUserGroupSequenceProvider implements DefaultGroupSequenceProvider<UpdateUserRequest> {
@Override
public List<Class<?>> getValidationGroups(UpdateUserRequest updateUserRequest) {
List<Class<?>> defaultGroupSequence = new ArrayList();
defaultGroupSequence.add(UpdateUserRequest.class);
if (updateUserRequest != null) {
addIdentityValidationGroups(defaultGroupSequence, updateUserRequest.getIdentity());
addRegistrationProfileValidationGroups(defaultGroupSequence, updateUserRequest.getRegistrationProfile());
addProfileValidationGroups(defaultGroupSequence, updateUserRequest.getProfile());
}
return defaultGroupSequence;
}
@Override
protected void addIdentityValidationGroups(List<Class<?>> defaultGroupSequence, IdentityRequest identityRequest) {
if (identityRequest != null) {
//Add password validation
if (identityRequest.getPassword() != null) {
defaultGroupSequence.add(IdentityValidation.PasswordValidation.class);
}
//Add email validation
if (identityRequest.getEmail() != null) {
defaultGroupSequence.add(IdentityValidation.EmailValidation.class);
}
}
}
}