Java Bean Validation: GroupSequence with Class Level Constraints

I have a bean class with several (customizable) internal constraints and one class level constraint. I would like to check the internal constraints before the class level constraint. The code is as follows:

@GroupSequence({ Inner.class, NewSlotBean.class }) @TotalBeanValid(groups = NewSlotBean.class) public class NewSlotBean { @DayMonthYearString(groups = Inner.class) private String slotDay; @TimeString(groups = Inner.class) private String slotBegin; @LengthString(groups = Inner.class) private String slotLength; } 

( Inner is just an empty interface lying somewhere).

However, when I try to run this, the class level restriction is not checked at all. When I try to define GroupSequence, for example

 @GroupSequence({ Inner.class, Outer.class }) 

(with Outer is a random interface), I get an exception:

 javax.validation.GroupDefinitionException: ...*.beans.NewSlotBean must be part of the redefined default group sequence. 

Does s / o know how to make sure class level constraints are checked after internal ones? (It seems that this was not the default! I had occasional problems with it that appear after a while.)

+6
source share
1 answer

Try the following:

 @GroupSequence({ Inner.class, NewSlotBean.class }) @TotalBeanValid(groups = Default.class) public class NewSlotBean { @DayMonthYearString(groups = Inner.class) private String slotDay; @TimeString(groups = Inner.class) private String slotBegin; @LengthString(groups = Inner.class) private String slotLength; } 

According to the specification, NewSlotBean is only a backup for the default group. See also section 3.4.3 of the Bean specification:

Since sequences cannot have circular dependencies, using the default value in a sequence declaration is not an option. Constraints, class A, and belonging to the default group (by default or explicitly) implicitly belong to group A.

The sequence defined in class A (that is, the redefinition of the default groups for the class) must contain group A. In other words, the default constraints placed in the class must be part of the definition of the sequence. If @GroupSequence overrides the default group for class A, does not contain group A, a GroupDefinitionException exception occurs when the Declaration of Constraints and the validation of the class is validated or when its metadata is requested.

+10
source

Source: https://habr.com/ru/post/914496/


All Articles