mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	add 新增自定义字典值校验器
This commit is contained in:
		@@ -84,4 +84,13 @@ public interface DictService {
 | 
			
		||||
     */
 | 
			
		||||
    List<DictDataDTO> getDictData(String dictType);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 校验指定的字典类型下是否存在指定的字典值
 | 
			
		||||
     *
 | 
			
		||||
     * @param dictType  字典类型
 | 
			
		||||
     * @param dictValue 字典值
 | 
			
		||||
     * @return true 表示该字典值在指定字典类型中有效;false 表示无效
 | 
			
		||||
     */
 | 
			
		||||
    Boolean isValidDictValue(String dictType, String dictValue);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,35 @@
 | 
			
		||||
package org.dromara.common.core.validate.dict;
 | 
			
		||||
 | 
			
		||||
import jakarta.validation.Constraint;
 | 
			
		||||
import jakarta.validation.Payload;
 | 
			
		||||
 | 
			
		||||
import java.lang.annotation.ElementType;
 | 
			
		||||
import java.lang.annotation.Retention;
 | 
			
		||||
import java.lang.annotation.RetentionPolicy;
 | 
			
		||||
import java.lang.annotation.Target;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 字典项校验注解
 | 
			
		||||
 *
 | 
			
		||||
 * @author AprilWind
 | 
			
		||||
 */
 | 
			
		||||
@Constraint(validatedBy = DictValueValidator.class)
 | 
			
		||||
@Target({ElementType.FIELD, ElementType.PARAMETER})
 | 
			
		||||
@Retention(RetentionPolicy.RUNTIME)
 | 
			
		||||
public @interface DictValue {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 字典类型,如 "sys_user_sex"
 | 
			
		||||
     */
 | 
			
		||||
    String dictType();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 默认校验失败提示信息
 | 
			
		||||
     */
 | 
			
		||||
    String message() default "字典值无效";
 | 
			
		||||
 | 
			
		||||
    Class<?>[] groups() default {};
 | 
			
		||||
 | 
			
		||||
    Class<? extends Payload>[] payload() default {};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,42 @@
 | 
			
		||||
package org.dromara.common.core.validate.dict;
 | 
			
		||||
 | 
			
		||||
import jakarta.validation.ConstraintValidator;
 | 
			
		||||
import jakarta.validation.ConstraintValidatorContext;
 | 
			
		||||
import org.dromara.common.core.service.DictService;
 | 
			
		||||
import org.dromara.common.core.utils.SpringUtils;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 自定义字典值校验器
 | 
			
		||||
 *
 | 
			
		||||
 * @author AprilWind
 | 
			
		||||
 */
 | 
			
		||||
public class DictValueValidator implements ConstraintValidator<DictValue, String> {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 字典类型
 | 
			
		||||
     */
 | 
			
		||||
    private String dictType;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 初始化校验器,提取注解上的字典类型
 | 
			
		||||
     *
 | 
			
		||||
     * @param annotation 注解实例
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void initialize(DictValue annotation) {
 | 
			
		||||
        this.dictType = annotation.dictType();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 校验字段值是否为指定字典类型中的合法值
 | 
			
		||||
     *
 | 
			
		||||
     * @param value   被校验的字段值
 | 
			
		||||
     * @param context 校验上下文(可用于构建错误信息)
 | 
			
		||||
     * @return true 表示校验通过(合法字典值),false 表示不通过
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isValid(String value, ConstraintValidatorContext context) {
 | 
			
		||||
        return SpringUtils.getBean(DictService.class).isValidDictValue(dictType, value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,37 +1,37 @@
 | 
			
		||||
package org.dromara.common.core.validate.enumd;
 | 
			
		||||
 | 
			
		||||
import jakarta.validation.ConstraintValidator;
 | 
			
		||||
import jakarta.validation.ConstraintValidatorContext;
 | 
			
		||||
import org.dromara.common.core.utils.StringUtils;
 | 
			
		||||
import org.dromara.common.core.utils.reflect.ReflectUtils;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 自定义枚举校验注解实现
 | 
			
		||||
 *
 | 
			
		||||
 * @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) {
 | 
			
		||||
        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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
package org.dromara.common.core.validate.enumd;
 | 
			
		||||
 | 
			
		||||
import jakarta.validation.ConstraintValidator;
 | 
			
		||||
import jakarta.validation.ConstraintValidatorContext;
 | 
			
		||||
import org.dromara.common.core.utils.StringUtils;
 | 
			
		||||
import org.dromara.common.core.utils.reflect.ReflectUtils;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 自定义枚举校验注解实现
 | 
			
		||||
 *
 | 
			
		||||
 * @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) {
 | 
			
		||||
        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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -294,4 +294,24 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService, DictService
 | 
			
		||||
        return BeanUtil.copyToList(list, DictDataDTO.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 校验指定的字典类型下是否存在指定的字典值
 | 
			
		||||
     *
 | 
			
		||||
     * @param dictType  字典类型
 | 
			
		||||
     * @param dictValue 字典值
 | 
			
		||||
     * @return true 表示该字典值在指定字典类型中有效;false 表示无效
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public Boolean isValidDictValue(String dictType, String dictValue) {
 | 
			
		||||
        if (StringUtils.isBlank(dictType) || StringUtils.isBlank(dictValue)) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        List<SysDictDataVo> datas = SpringUtils.getAopProxy(this).selectDictDataByType(dictType);
 | 
			
		||||
        if (CollUtil.isEmpty(datas)) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        Map<String, String> map = StreamUtils.toMap(datas, SysDictDataVo::getDictValue, SysDictDataVo::getDictLabel);
 | 
			
		||||
        return map.containsKey(dictValue);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user