add 新增 validation支持枚举校验
Signed-off-by: 秋辞未寒 <545073804@qq.com>
This commit is contained in:
parent
5e6cb0dd3c
commit
9e613488f1
@ -0,0 +1,39 @@
|
|||||||
|
package org.dromara.common.core.validate.enumd;
|
||||||
|
|
||||||
|
import jakarta.validation.Constraint;
|
||||||
|
import jakarta.validation.Payload;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义枚举校验注解
|
||||||
|
*
|
||||||
|
* @author 秋辞未寒
|
||||||
|
* @date 2024-12-09
|
||||||
|
*/
|
||||||
|
@Documented
|
||||||
|
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@Repeatable(EnumPattern.List.class) // 允许在同一元素上多次使用该注解
|
||||||
|
@Constraint(validatedBy = {EnumPatternValidator.class})
|
||||||
|
public @interface EnumPattern {
|
||||||
|
|
||||||
|
Class<? extends ValidateEnum> type() default ValidateEnum.class;
|
||||||
|
|
||||||
|
String message() default "输入值不在枚举范围内";
|
||||||
|
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
|
||||||
|
@Documented
|
||||||
|
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
@interface List {
|
||||||
|
EnumPattern[] value();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package org.dromara.common.core.validate.enumd;
|
||||||
|
|
||||||
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
|
import jakarta.validation.ConstraintValidator;
|
||||||
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
import jakarta.validation.ValidationException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义枚举校验注解实现
|
||||||
|
*
|
||||||
|
* @author 秋辞未寒
|
||||||
|
* @date 2024-12-09
|
||||||
|
*/
|
||||||
|
public class EnumPatternValidator implements ConstraintValidator<EnumPattern, String> {
|
||||||
|
|
||||||
|
private EnumPattern annotation;;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(EnumPattern annotation) {
|
||||||
|
ConstraintValidator.super.initialize(annotation);
|
||||||
|
this.annotation = annotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
|
||||||
|
try {
|
||||||
|
if (StringUtils.isNotBlank(value)) {
|
||||||
|
Class<? extends ValidateEnum> type = annotation.type();
|
||||||
|
ValidateEnum[] constants = type.getEnumConstants();
|
||||||
|
for (ValidateEnum e : constants) {
|
||||||
|
if (e.validate(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new ValidationException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package org.dromara.common.core.validate.enumd;
|
||||||
|
|
||||||
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举类型校验接口
|
||||||
|
*
|
||||||
|
* @author 秋辞未寒
|
||||||
|
* @date 2024-12-09
|
||||||
|
*/
|
||||||
|
public interface ValidateEnum {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取枚举code
|
||||||
|
* <pre>该code用于匹配</pre>
|
||||||
|
* @return 枚举code
|
||||||
|
*/
|
||||||
|
String getCode();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验枚举code
|
||||||
|
* @param code 枚举code
|
||||||
|
* @return 校验结果
|
||||||
|
*/
|
||||||
|
default boolean validate(String code) {
|
||||||
|
return StringUtils.equals(code, getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user