Close

JavaBean Validation - Predefined constraints

[Updated: Aug 6, 2017, Created: Sep 23, 2016]

JavaBean validation specification defines following constraint annotations:

Annotation Elements: required and important optional Target variable type Expected target variable value
AssertFalse
AssertTrue
None boolean, java.lang.Boolean false
true
DecimalMin
DecimalMax
(value=int, inclusive=boolean) BigDecimal, BigInteger, CharSequence, primitive numbers (byte, short, int, long) and their respective wrappers variableValue >= value
variableValue <= value
Note: above is not inclusive (there's no '=') if inclusive=false, by default it's true.
Digits (integer=int, fraction=int) same as above variableIntegralDigits <= integer
and
variableFractionalDigits <= fraction
Min
Max
(value=int) same as above, except for CharSequence variableValue >= value
variableValue <= value
Size (max=int, min=int) CharSequence, Collection, Map, Array variableValueRelatedSize >= min
and
variableValueRelatedSize <= max
Pattern (regexp=String, flags=Pattern.Flag[]) CharSequence (String) Pattern.matches(regexp, variableValue) == true
Past
Future
None java.util.Date, java.util.Calendar currentMillis of variableValue < time now in millis
currentMillis of variableValue > time now in millis
Null
NotNull
None Any subclass of java.lang.Object variableValue == null
variableValue != null


All above constraints are defined in the package javax.validation.constraints

Other optional elements of constraint annotations

All above annotations including which are showing 'None' for the elements, have these additional elements:

  • message=String:

    The validation error to be displayed as a message.

    Defaulted to built-in resource bundle keys for i18n purpose.

    The default can be overridden by a hard coded message string literal or a new application defined resource bundle key.

    Resource keys are used within {}.

    The default resource key is the fully qualified name of the annotation class concatenated to .message, e.g. javax.validation.constraints.Future.message. we should follow the same convention when creating custom constraint annotations.


  • groups=Class<?>[]

    This is a way to group constraints. The validation evaluation can be restricted to certain group(s) of annotation based on the class types defined here. We can specify groups while calling Validator#validate(..) method.


  • payload=Class<? extends Payload>[]

    This is a way to associate some metadata information with a given constraint annotation declaration.


Note that all above three elements are optional on client side but they must be defined in each constraint annotations definition. So when creating new custom constraint annotations, we have to include all these three element definitions as it is, otherwise we will have javax.validation.ConstraintDefinitionException.



List constraints

For each of the above predefined annotations, there is an inner annotation type named List.

For example let's have a look at NotNull.java

@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { })
 public @interface NotNull {
	String message() default "{javax.validation.constraints.NotNull.message}";
	Class<?>[] groups() default { };
	Class<? extends Payload>[] payload() default { };
   
	@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
	@Retention(RUNTIME)
	@Documented
	@interface List {
	   NotNull[] value();
	}
}

This annotation generally allows to specify several annotations on the same element, e.g. with different validation groups and messages.



See Also