add 新增自定义字典值校验器
This commit is contained in:
parent
c054029cfc
commit
737838d92f
@ -84,4 +84,13 @@ public interface DictService {
|
|||||||
*/
|
*/
|
||||||
List<DictDataDTO> getDictData(String dictType);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -13,7 +13,7 @@ import org.dromara.common.core.utils.reflect.ReflectUtils;
|
|||||||
*/
|
*/
|
||||||
public class EnumPatternValidator implements ConstraintValidator<EnumPattern, String> {
|
public class EnumPatternValidator implements ConstraintValidator<EnumPattern, String> {
|
||||||
|
|
||||||
private EnumPattern annotation;;
|
private EnumPattern annotation;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(EnumPattern annotation) {
|
public void initialize(EnumPattern annotation) {
|
||||||
|
@ -294,4 +294,24 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService, DictService
|
|||||||
return BeanUtil.copyToList(list, DictDataDTO.class);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user