Close

JavaBean Validation - Predefined constraints

[Updated: Jun 26, 2018, Created: Sep 23, 2016]

JavaBean validation specification defines following constraint annotations:

Annotation Elements: required and important optional Target variable type Expected target variable value
(variableValue)
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
java.time.*(2.0)
currentMillis of variableValue < time now in millis
currentMillis of variableValue > time now in millis
PastOrPresent
FutureOrPresent
(2.0)
None java.util.Date, java.util.Calendar, java.time.* types currentMillis of variableValue <=0 time now in millis
currentMillis of variableValue >= time now in millis
Null
NotNull
None Any subclass of java.lang.Object variableValue == null
variableValue != null
Email
(2.0)
None CharSequence (String) must be a valid email address.
NotEmpty
(2.0)
None CharSequence (String)
Collection
Map
Array
must not be null nor empty.
NotBlank
(2.0)
None CharSequence (String) must not be null and must contain at least one non-whitespace character.
Positive
Negative
(2.0)
None BigDecimal
BigInteger
byte, short, int, long, float, double and their respective wrappers
variableValue > 0
variableValue < 0

PositiveOrZero
NegativeOrZero
(2.0)
None BigDecimal
BigInteger
byte, short, int, long, float, double and their respective wrappers
variableValue >=0
variableValue <=0



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.



Bean Validation 2.0 (JSR 380) Changes

  • New built-in constraints (as seen in above table):
    @Email, @NotEmpty, @NotBlank, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero, @PastOrPresent and @FutureOrPresent
  • All constraint annotations have been annotated with Java 8 @Repeatable annotation.
  • All constraint annotation can be used as Java 8 Type Annotations, i.e. their @Target now includes ElementType.TYPE_USE.

See Also