rdm
rdm

Reputation: 330

Validation Group inheritance

Given the following classes and interfaces

class Person {

  @NotNull(groups=Both.class)
  private String name;
}

Validate groups:

interface One {}
interface Two {}
interface Both extends One, Two {}

When calling

Person person = new Person();

validator.validate(person, One.class) 

or

validator.validate(person, Two.class)  

I would expect that it should be invalid since name is null but it does not. It turns out the group Both.class is only useful when used in validator.validate(..., Both.class) method.

Upvotes: 2

Views: 2420

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

You got the inheritance in the wrong direction. The Both group extends One and Two You might think about it as "Both contains One and Two". That means that, everytime you validate against the Both group, all the constraints of groups One and Two are also contained into Both, and are thus validated.

But the inverse is not true: a constraint belonging to the group Both is not part of One, and is not part of Two either. So, if you validate the constraints of group One, constraints belonging to Both won't be validated.

See http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-groupsequence-groupinheritance for the reference.

Upvotes: 7

Related Questions