Search code examples
javavalidationinheritancebean-validationhibernate-validator

Bean Validation Group inheritance isn't working with Group Sequence Provider


I'm currently working on implementing an application that stores contact information which contains both foreign and domestic addresses, but the addresses can also be disabled.

I have attempted to define three validation groups: a super group for the always validated info and two child classes for the different address states.

public interface ContactValidation {}
public interface DomesticValidation extends ContactValidation {}
public interface ForeignValidation extends ContactValidation {}

I then defined an @GroupSequenceProvider on the contact class.

public class ContactGroupSequenceProvider implements DefaultGroupSequenceProvider<Contact> {

@Override
public List<Class<?>> getValidationGroups(Contact contact) {
    List<Class<?>> defaultGroupSequence = new ArrayList<Class<?>>();

    if(contact.getIsForeign()){
        defaultGroupSequence.add(Contact.ForeignValidation.class);
    } else {
        defaultGroupSequence.add(Contact.DomesticValidation.class);
    } //TODO: Add Address Disabled Case

    defaultGroupSequence.add(Contact.class); //Get a GroupDefinitionException without this line

    return defaultGroupSequence;
}

When I run the program with either address validation, the fields marked with groups=Contact.ContactValidation.class are not being validated even though it is a super class of the address validations.

I need this application to validate together so the used doesn't have to fix an error, submit and get the new errors.

I also would like to avoid solutions with the always validated fields being like this.

@NotBlank(groups={DomesticValidation.class,ForeignValidation.class ,ContactValidation.class})   
private String fullContactName;

Any advice appreciated!


Solution

  • If you return one of the sub-classes, I'd also expect the constraints tagged with ContactValidation to be validated. This seems to be a bug in Hibernate Validator, so could you open a ticket in our issue tracker?

    As a work-around you might try to explicitly add ContactValidation to the list you return.