Annotation Interface Range


@Documented @Constraint(validatedBy={}) @Target({METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE}) @Retention(RUNTIME) public @interface Range
The annotated element must be in the specified range, which can include both a minimum and maximum, only a minimum, or only a maximum. When only minimum or maximum is specified, the range is open-ended on the non-specified side. For example, if only a minimum is specified then there is no maximum applied in the constraint validation.

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 types
  • float, double, and their respective wrapper types
  • BigDecimal
  • BigInteger
  • 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
While 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 Range must contain a minimum or a maximum (or both). If no minimum or maximum is supplied, a IllegalStateException is thrown at runtime during construction of the RangeValidator. This is because this constraint supports various types and not just a single type like Hibernate Validator's Range constraint annotation; that annotation supports only numeric types and therefore can set default values for min and max. 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 the RangeValidator is instantiated.
  • 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 allowNull
      Whether 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:
      ""