Java Reference
In-Depth Information
Specifying the Order of Group Validation
By default, the Bean Validation API will validate each constraint within a group in no particular order. In some cases,
it may make sense to define an explicit ordering for validation, such as when one validation depends upon another.
In such cases, explicit ordering can be implemented by creating a group sequence. To define a group sequence,
declare a Java interface to create a new validation group, and then annotate that interface with the
@GroupSequence
annotation, specifying validation groups within the annotation in the order in which they should be validated.
If a constraint that belongs to one group fails, then the validation halts, and all subsequent groups are not validated.
For example, the following group, named
MasterAuthentication
, specifies a group sequence in which all
constraints belonging to the
Authentication
group are first validated, followed by the constraints belonging to the
Authentication2
group:
@GroupSequence({Authentication.class, Authentication2.class})
public interface MasterAuthentication {}
■
if a group sequence is defined and there is a circular dependency with another sequence or group,
then a
GroupDefinitionException
is raised.
Note
Group Conversion
The brief overview that was given in the previous sections will help you understand group conversion, which is a new
feature of Bean Validation 1.1. It is now possible to alter a validation group specification to use a group different from
the one originally requested. To do so, specify the
@ConvertGroup
annotation, which can be used anywhere that
@Valid
can be used. If used without specifying the
@Valid
annotation, then a
ConstraintDeclarationException
is raised.
The
@Valid
annotation indicates that validation is to be propagated and groups are passed as nested elements
unless the
@ConvertGroup
annotation is specified. You can think of group conversion as a conditional element of
group validation. For instance, in one circumstance it may make sense to utilize one group of validations, whereas
in another circumstance it may make sense to utilize a different group of validations. Let's say you wanted to apply a
stronger authentication mechanism to administrator users and apply a lesser authentication mechanism to standard
users. You could do so by indicating which validation group to use under each circumstance using group conversion.
To use this technique, specify the
@ConvertGroup
annotation on any field that may have multiple group
designations, depending upon the circumstance. Specify the group that should be indicated by using the
from
attribute of the
@ConvertGroup
annotation, and specify the group that should be converted to with the
to
attribute.
To indicate more than one case of group conversion, specify the
@ConvertGroup.List
annotation, and specify a
List
of
@ConvertGroup
annotations.
Perhaps the easiest way to understand this concept is to see an example. In the following example, the
User
group is going to utilize the
Authentication
validation group if a
Default
group is specified (standard user), and it
will utilize the
Authentiation2
validation group if a
Admin
group is specified:
public class User {
@Valid
@ConvertGroup.List( {
@ConvertGroup(from=Default.class, to=Authentication.class),
@ConvertGroup(from=Admin.class, to=Authentication2.class)
} )
private String getPassword() { ... }
}