mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 02:58:38 +08:00
feat(validation): 增强 EnumValue 枚举校验器,支持枚举值的数组和集合校验,增加对 BaseEnum 接口的支持
This commit is contained in:
@@ -18,26 +18,24 @@ package top.continew.starter.validation.autoconfigure;
|
|||||||
|
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import jakarta.validation.Validator;
|
import jakarta.validation.Validator;
|
||||||
import org.hibernate.validator.HibernateValidator;
|
import org.hibernate.validator.BaseHibernateValidatorConfiguration;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||||
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSR 303 校验器自动配置
|
* JSR 303 校验器自动配置
|
||||||
*
|
*
|
||||||
* @author Charles7c
|
* @author Charles7c
|
||||||
* @since 2.3.0
|
* @since 2.3.0
|
||||||
*/
|
*/
|
||||||
@AutoConfiguration
|
@AutoConfigureBefore
|
||||||
public class ValidatorAutoConfiguration {
|
public class ValidationAutoConfiguration {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(ValidatorAutoConfiguration.class);
|
private static final Logger log = LoggerFactory.getLogger(ValidationAutoConfiguration.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validator 失败立即返回模式配置
|
* Validator 失败立即返回模式配置
|
||||||
@@ -51,10 +49,9 @@ public class ValidatorAutoConfiguration {
|
|||||||
try (LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean()) {
|
try (LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean()) {
|
||||||
// 国际化
|
// 国际化
|
||||||
factoryBean.setValidationMessageSource(messageSource);
|
factoryBean.setValidationMessageSource(messageSource);
|
||||||
factoryBean.setProviderClass(HibernateValidator.class);
|
// 快速失败
|
||||||
Properties properties = new Properties();
|
factoryBean.getValidationPropertyMap()
|
||||||
properties.setProperty("hibernate.validator.fail_fast", "true");
|
.put(BaseHibernateValidatorConfiguration.FAIL_FAST, Boolean.TRUE.toString());
|
||||||
factoryBean.setValidationProperties(properties);
|
|
||||||
factoryBean.afterPropertiesSet();
|
factoryBean.afterPropertiesSet();
|
||||||
return factoryBean.getValidator();
|
return factoryBean.getValidator();
|
||||||
}
|
}
|
||||||
@@ -62,6 +59,6 @@ public class ValidatorAutoConfiguration {
|
|||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void postConstruct() {
|
public void postConstruct() {
|
||||||
log.debug("[ContiNew Starter] - Auto Configuration 'Validator' completed initialization.");
|
log.debug("[ContiNew Starter] - Auto Configuration 'Validation' completed initialization.");
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -22,10 +22,10 @@ import jakarta.validation.ConstraintValidator;
|
|||||||
import jakarta.validation.ConstraintValidatorContext;
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import top.continew.starter.core.enums.BaseEnum;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 枚举校验器
|
* 枚举校验器
|
||||||
@@ -53,17 +53,53 @@ public class EnumValueValidator implements ConstraintValidator<EnumValue, Object
|
|||||||
if (value == null) {
|
if (value == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理数组场景
|
||||||
|
if (value.getClass().isArray()) {
|
||||||
|
Object[] array = (Object[])value;
|
||||||
|
for (Object element : array) {
|
||||||
|
if (!isValidElement(element)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理集合场景
|
||||||
|
if (value instanceof Iterable<?> iterable) {
|
||||||
|
for (Object element : iterable) {
|
||||||
|
if (!isValidElement(element)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理单个值场景
|
||||||
|
return isValidElement(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验单个元素是否有效
|
||||||
|
*
|
||||||
|
* @param value 待校验的值
|
||||||
|
* @return 是否有效
|
||||||
|
*/
|
||||||
|
private boolean isValidElement(Object value) {
|
||||||
// 优先校验 enumValues
|
// 优先校验 enumValues
|
||||||
if (enumValues.length > 0) {
|
if (enumValues.length > 0) {
|
||||||
return Arrays.asList(enumValues).contains(Convert.toStr(value));
|
return Arrays.asList(enumValues).contains(Convert.toStr(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
Enum[] enumConstants = enumClass.getEnumConstants();
|
Enum[] enumConstants = enumClass.getEnumConstants();
|
||||||
if (enumConstants.length == 0) {
|
if (enumConstants.length == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CharSequenceUtil.isBlank(enumMethod)) {
|
if (CharSequenceUtil.isBlank(enumMethod)) {
|
||||||
return findEnumValue(enumConstants, Enum::toString, Convert.toStr(value));
|
return findEnumValue(enumConstants, Convert.toStr(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 枚举类指定了方法名,则调用指定方法获取枚举值
|
// 枚举类指定了方法名,则调用指定方法获取枚举值
|
||||||
Method method = enumClass.getMethod(enumMethod);
|
Method method = enumClass.getMethod(enumMethod);
|
||||||
@@ -82,16 +118,19 @@ public class EnumValueValidator implements ConstraintValidator<EnumValue, Object
|
|||||||
* 遍历枚举类,判断是否包含指定值
|
* 遍历枚举类,判断是否包含指定值
|
||||||
*
|
*
|
||||||
* @param enumConstants 枚举类数组
|
* @param enumConstants 枚举类数组
|
||||||
* @param function 获取枚举值的函数
|
|
||||||
* @param value 待校验的值
|
* @param value 待校验的值
|
||||||
* @return 是否包含指定值
|
* @return 是否包含指定值
|
||||||
*/
|
*/
|
||||||
private boolean findEnumValue(Enum[] enumConstants, Function<Enum, Object> function, Object value) {
|
private boolean findEnumValue(Enum[] enumConstants, Object value) {
|
||||||
for (Enum enumConstant : enumConstants) {
|
for (Enum enumConstant : enumConstants) {
|
||||||
if (function.apply(enumConstant).equals(value)) {
|
if (enumConstant instanceof BaseEnum<?> baseEnum) {
|
||||||
|
if (baseEnum.getValue().toString().equals(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else if (enumConstant.toString().equals(value)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1 +1 @@
|
|||||||
top.continew.starter.validation.autoconfigure.ValidatorAutoConfiguration
|
top.continew.starter.validation.autoconfigure.ValidationAutoConfiguration
|
Reference in New Issue
Block a user