review:@InDict 的实现,以及相关的使用案例

This commit is contained in:
YunaiV
2025-07-26 17:15:57 +08:00
parent f05a66f8e4
commit c25d1b3719
4 changed files with 14 additions and 8 deletions

View File

@@ -25,7 +25,7 @@ import java.lang.annotation.Target;
public @interface InDict {
/**
* 数据字典type
* 数据字典 type
*/
String type();

View File

@@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.dict.validation;
import cn.iocoder.yudao.framework.common.util.string.StrUtils;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
@@ -19,19 +19,22 @@ public class InDictCollectionValidator implements ConstraintValidator<InDict, Co
@Override
public boolean isValid(Collection<?> list, ConstraintValidatorContext context) {
if (list == null) {
// 为空时,默认不校验,即认为通过
if (CollUtil.isEmpty(list)) {
return true;
}
// 校验全部通过
List<String> dbValues = DictFrameworkUtils.getDictDataValueList(dictType);
boolean match = list.stream().allMatch(v -> dbValues.stream()
.anyMatch(dbValue -> dbValue.equalsIgnoreCase(v.toString())));
if (match) {
return true;
}
// 校验不通过,自定义提示语句
context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值
context.buildConstraintViolationWithTemplate(
context.getDefaultConstraintMessageTemplate().replaceAll("\\{value}",dbValues.toString())
context.getDefaultConstraintMessageTemplate().replaceAll("\\{value}", dbValues.toString())
).addConstraintViolation(); // 重新添加错误提示语句
return false;
}

View File

@@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.dict.validation;
import cn.iocoder.yudao.framework.common.util.string.StrUtils;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
@@ -24,10 +24,11 @@ public class InDictValidator implements ConstraintValidator<InDict, Object> {
}
// 校验通过
final List<String> values = DictFrameworkUtils.getDictDataValueList(dictType);
boolean match = values.stream().anyMatch(v -> v.equalsIgnoreCase(value.toString()));
boolean match = values.stream().anyMatch(v -> StrUtil.equalsIgnoreCase(v, value.toString()));
if (match) {
return true;
}
// 校验不通过,自定义提示语句
context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值
context.buildConstraintViolationWithTemplate(