JSR-303 validation groups define a default group

Geoffrey De Vylder picture Geoffrey De Vylder · Feb 12, 2016 · Viewed 8.7k times · Source

I have a bean that has a lot of fields annotated with JSR-303 validation annotations. There is a new requirement now that one of the fields is mandatory, but only in certain conditions.

I looked around and have found what I needed, validation groups.

This is what I have now:

public interface ValidatedOnCreationOnly {
}

@NotNull(groups = ValidatedOnCreationOnly.class)
private String employerId;
@Length(max = 255)
@NotNull
private String firstName;
@Length(max = 255)
@NotNull
private String lastName;

However, when I run this validation in a unit test:

@Test
public void testEmployerIdCanOnlyBeSetWhenCreating() {
    EmployeeDTO dto = new EmployeeDTO();

    ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
    Set<ConstraintViolation<EmployeeDTO>> violations = vf.getValidator().validate(dto, EmployeeDTO.ValidatedOnCreationOnly.class);

    assertEquals(violations.size(), 3);
}

It turns out that all of the non-group annotated validations are ignored and I get only 1 violation.

I can understand this behaviour but I would like to know if there is a way I can make the group include all non-annotated parameters as well. If not I'd have to do something like this:

public interface AlwaysValidated {
}

public interface ValidatedOnCreationOnly extends AlwaysValidated {
}

@NotNull(groups = ValidatedOnCreationOnly.class)
private String employerId;
@Length(max = 255, groups = AlwaysValidated.class)
@NotNull(groups = AlwaysValidated.class)
private String firstName;
@Length(max = 255, groups = AlwaysValidated.class)
@NotNull(groups = AlwaysValidated.class)
private String lastName;

The real class I'm working with has a lot more fields (about 20), so this method turns what was a clear way of indicating the validations into a big mess.

Can anyone tell me if there is a better way? Maybe something like:

vf.getValidator().validate(dto, EmployeeDTO.ValidatedOnCreationOnly.class, NonGroupSpecific.class);

I'm using this in a spring project so if spring has another way I'll be glad to know.

Answer

Ali Dehghani picture Ali Dehghani · Feb 12, 2016

There is a Default group in javax.validation.groups.Default, which represents the default Bean Validation group. Unless a list of groups is explicitly defined:

  • constraints belong to the Default group
  • validation applies to the Default group

You could extends this group:

public interface ValidatedOnCreationOnly extends Default {}