Annotation Interface InEnum


@Documented @Constraint(validatedBy={}) @Target({METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE}) @Retention(RUNTIME) public @interface InEnum
The annotated element must have a value in the specified enum class.

For example, if you have a String property that you want to constrain to one of the values in an enum, you can apply this annotation, and specify the enum class to validate against.

By default, does not permit null values. If the element being validated permits nulls, you can set allowNull() to true.

You can optionally perform case-insensitive validation using ignoreCase() or specify a custom method using valueMethod() which provides the values to validate against.

Examples:

 @InEnum(enumClass = Season.class)
  private String season;
 
 @InEnum(enumClass = Season.class, allowNull = true, ignoreCase = true)
  private String season;
 
 @InEnum(enumClass = Season.class, ignoreCase = true, valueMethod = "humanizedValue")
  private String season;
 
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    Class<? extends Enum<?>>
     
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Whether to consider null as valid.
    Class<?>[]
     
    boolean
    Whether to ignore case.
     
    Class<? extends jakarta.validation.Payload>[]
     
    By default, InEnum uses the enum constants as the values to validate against.
  • Element Details

    • enumClass

      Class<? extends Enum<?>> enumClass
      Returns:
      the enum class containing the allowable values for the annotated element
    • message

      String message
      Default:
      "{org.kiwiproject.validation.InEnum.message}"
    • groups

      Class<?>[] groups
      Default:
      {}
    • payload

      Class<? extends jakarta.validation.Payload>[] payload
      Default:
      {}
    • allowNull

      boolean allowNull
      Whether to consider null as valid. The default is false.
      Returns:
      true to consider null as valid
      Default:
      false
    • ignoreCase

      boolean ignoreCase
      Whether to ignore case. By default, match is case-sensitive.
      Returns:
      true to ignore case, false to perform a case-sensitive match.
      Default:
      false
    • valueMethod

      String valueMethod
      By default, InEnum uses the enum constants as the values to validate against. If there is a specific method in the enum which provides a String value that should be used for validation instead of the enum constants, specify it here.

      For example, if you have a Season enum with values like FALL and SPRING, but the values specified by users are more human-friendly, such as "Fall" and "Spring", and are available via a method named humanizedValue, then the annotation should be specified as follows:

       @InEnum(enumClass = Season.class, valueMethod = "humanizedValue")
        private String season;
       
      Returns:
      the name of the method that returns the String value to perform validation against. By default, this is an empty String, which means the enum constant will be used.
      Default:
      ""