Annotation Interface InEnum
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 -
Optional Element Summary
Optional Elements
-
Element Details
-
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 allowNullWhether to consider null as valid. The default is false.- Returns:
- true to consider null as valid
- Default:
- false
-
ignoreCase
boolean ignoreCaseWhether 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 valueMethodBy default,InEnumuses 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
Seasonenum with values likeFALLandSPRING, but the values specified by users are more human-friendly, such as "Fall" and "Spring", and are available via a method namedhumanizedValue, 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:
- ""
-