Annotation Interface Range
Null values are considered valid by default, but you can change this behavior using the allowNull()
property.
Use min() and max() to specify the minimum and maximum values allowed in the range. These
are inclusive values, e.g. for a range with a minimum of 5 and maximum of 10, the values 5 and 10 are
considered as part of the range. A valid range must contain a minimum, a maximum, or both. See the implementation
note below for why this can only be checked at runtime.
Use the minLabel() and maxLabel() to specify custom labels to use in place of the min and max
values. This is useful in cases where the min and max are large numbers or when validating date/time values where
the min and max are specified as milliseconds since the epoch. Note specifically that when both a minimum and a
maximum are supplied, and you want to use labels, then you should supply both the min and max labels.
The supported types are:
byte,short,int,long, and their respective wrapper typesfloat,double, and their respective wrapper typesBigDecimalBigInteger- Date, using epoch millis for the min and max values
- Instant, using epoch millis for the min and max values
- JSON, using JSON to define the min and max values
float and double are supported, be aware of the possibility for rounding errors when values
are near the range bounds. The comparisons use Float.compareTo(Float) and Double.compareTo(Double).- Implementation Note:
- A
Rangemust contain a minimum or a maximum (or both). If no minimum or maximum is supplied, aIllegalStateExceptionis thrown at runtime during construction of theRangeValidator. This is because this constraint supports various types and not just a single type like Hibernate Validator'sRangeconstraint annotation; that annotation supports only numeric types and therefore can set default values forminandmax. In contrast, there are no logical defaults which this constraint can provide so the default must be an empty string for code to compile. The only place to verify a minimum or maximum exists is therefore when theRangeValidatoris instantiated.
-
Optional Element Summary
Optional Elements
-
Element Details
-
message
String message- Default:
- "{org.kiwiproject.validation.Range.between.message.minMaxValues}"
-
groups
Class<?>[] groups- Default:
- {}
-
payload
Class<? extends jakarta.validation.Payload>[] payload- Default:
- {}
-
allowNull
boolean allowNullWhether to consider null as valid. The default is true.- Returns:
- true to consider null as valid
- Default:
- true
-
min
String min- Returns:
- the minimum allowed value for this range
- Default:
- ""
-
minLabel
String minLabel- Returns:
- the label to be used in error messages in place of the minimum value, e.g. "ten" instead of 10
- Default:
- ""
-
max
String max- Returns:
- the maximum allowed value for this range
- Default:
- ""
-
maxLabel
String maxLabel- Returns:
- the label to be used in error messages in place of the maximum value, e.g. "ten" instead of 10
- Default:
- ""
-