mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-05 01:47:17 +00:00
update 优化 EnumPattern 支持任意类型校验
This commit is contained in:
+33
-7
@@ -1,5 +1,7 @@
|
|||||||
package org.dromara.common.core.validate.enums;
|
package org.dromara.common.core.validate.enums;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.util.BooleanUtil;
|
||||||
import jakarta.validation.ConstraintValidator;
|
import jakarta.validation.ConstraintValidator;
|
||||||
import jakarta.validation.ConstraintValidatorContext;
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
@@ -14,12 +16,17 @@ import java.util.Set;
|
|||||||
* @author 秋辞未寒
|
* @author 秋辞未寒
|
||||||
* @date 2024-12-09
|
* @date 2024-12-09
|
||||||
*/
|
*/
|
||||||
public class EnumPatternValidator implements ConstraintValidator<EnumPattern, String> {
|
public class EnumPatternValidator implements ConstraintValidator<EnumPattern, Object> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 枚举允许值集合。
|
* 枚举允许值集合。
|
||||||
*/
|
*/
|
||||||
private final Set<String> values = new HashSet<>();
|
private final Set<Object> values = new HashSet<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举字段值类型。
|
||||||
|
*/
|
||||||
|
private Class<?> valueType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化枚举允许值集合。
|
* 初始化枚举允许值集合。
|
||||||
@@ -36,24 +43,43 @@ public class EnumPatternValidator implements ConstraintValidator<EnumPattern, St
|
|||||||
for (Object e : annotation.type().getEnumConstants()) {
|
for (Object e : annotation.type().getEnumConstants()) {
|
||||||
Object fieldValue = ReflectUtils.invokeGetter(e, fieldName);
|
Object fieldValue = ReflectUtils.invokeGetter(e, fieldName);
|
||||||
if (fieldValue != null) {
|
if (fieldValue != null) {
|
||||||
values.add(String.valueOf(fieldValue));
|
if (valueType == null) {
|
||||||
|
valueType = fieldValue.getClass();
|
||||||
|
}
|
||||||
|
values.add(fieldValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验字符串是否在枚举允许值集合内。
|
* 校验值是否在枚举允许值集合内。
|
||||||
|
*
|
||||||
|
* <p>以枚举字段的实际类型转换输入值后进行比较,可兼容字符串、数字、布尔值等字段类型,
|
||||||
|
* 同时避免将无法转换的值视为合法值。字符串类型的空白值按未填写处理,其他类型仅 null
|
||||||
|
* 按未填写处理。</p>
|
||||||
*
|
*
|
||||||
* @param value 待校验值
|
* @param value 待校验值
|
||||||
* @param constraintValidatorContext 校验上下文
|
* @param constraintValidatorContext 校验上下文
|
||||||
* @return true 校验通过 false 校验失败
|
* @return true 校验通过 false 校验失败
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
|
public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
|
||||||
if (StringUtils.isBlank(value)) {
|
if (value == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return values.contains(value);
|
if (valueType == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (value instanceof CharSequence && StringUtils.isBlank(value.toString())
|
||||||
|
&& CharSequence.class.isAssignableFrom(valueType)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ((valueType == Boolean.class || valueType == boolean.class) && value instanceof CharSequence
|
||||||
|
&& BooleanUtil.toBooleanObject(value.toString()) == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Object convertedValue = Convert.convertWithCheck(valueType, value, null, true);
|
||||||
|
return convertedValue != null && values.contains(convertedValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user