update 优化 pr代码
This commit is contained in:
parent
b1badca062
commit
552e543471
@ -24,13 +24,13 @@ public @interface EnumPattern {
|
||||
/**
|
||||
* 需要校验的枚举类型
|
||||
*/
|
||||
Class<? extends Enum> type() default Enum.class;
|
||||
Class<? extends Enum<?>> type();
|
||||
|
||||
/**
|
||||
* 枚举类型校验值字段名称
|
||||
* TODO 需确保该字段实现了 getter 方法
|
||||
* 需确保该字段实现了 getter 方法
|
||||
*/
|
||||
String fieldName() default "code";
|
||||
String fieldName();
|
||||
|
||||
String message() default "输入值不在枚举范围内";
|
||||
|
||||
|
@ -1,11 +1,9 @@
|
||||
package org.dromara.common.core.validate.enumd;
|
||||
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.core.utils.reflect.ReflectUtils;
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
import jakarta.validation.ValidationException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.core.utils.reflect.ReflectUtils;
|
||||
|
||||
/**
|
||||
* 自定义枚举校验注解实现
|
||||
@ -25,21 +23,15 @@ public class EnumPatternValidator implements ConstraintValidator<EnumPattern, St
|
||||
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
|
||||
try {
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
Class<?> type = annotation.type();
|
||||
String fieldName = annotation.fieldName();
|
||||
Object[] enumConstants = type.getEnumConstants();
|
||||
for (Object e : enumConstants) {
|
||||
if (value.equals(ReflectUtils.invokeGetter(e, fieldName))) {
|
||||
return true;
|
||||
}
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
String fieldName = annotation.fieldName();
|
||||
for (Object e : annotation.type().getEnumConstants()) {
|
||||
if (value.equals(ReflectUtils.invokeGetter(e, fieldName))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
throw new ValidationException(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user