Annotation Type InEnum
-
@Documented @Constraint(validatedBy=InEnumValidator.class) @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()
totrue
.You can optionally perform case-insensitive validation using
ignoreCase()
or specify a custom method usingvalueMethod()
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;
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description boolean
allowNull
Whether to consider null as valid.Class<?>[]
groups
boolean
ignoreCase
Whether to ignore case.String
message
Class<? extends javax.validation.Payload>[]
payload
String
valueMethod
By default,InEnum
uses the enum constants as the values to validate against.
-
-
-
-
message
String message
- Default:
- "{org.kiwiproject.validation.InEnum.message}"
-
-
-
groups
Class<?>[] groups
- Default:
- {}
-
-
-
payload
Class<? extends javax.validation.Payload>[] payload
- Default:
- {}
-
-
-
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 likeFALL
andSPRING
, 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:
- ""
-
-