🍅 feat(Dict): 增加字典多选校验逻辑

This commit is contained in:
喵你个汪呀 2025-08-17 12:45:36 +08:00
parent b2c56cd16e
commit 8a339f008f

View File

@ -10,13 +10,18 @@ namespace Admin.NET.Core;
/// 字典值合规性校验特性
/// </summary>
[SuppressSniffer]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class DictAttribute : ValidationAttribute, ITransient
{
/// <summary>
/// 字典编码
/// </summary>
public string DictTypeCode { get; }
public readonly string DictTypeCode;
/// <summary>
/// 是否多选
/// </summary>
public readonly bool Multiple;
/// <summary>
/// 是否允许空字符串
@ -29,109 +34,110 @@ public class DictAttribute : ValidationAttribute, ITransient
public bool AllowNullValue { get; set; } = false;
/// <summary>
/// 字典值合规性校验特性
/// 构造函数
/// </summary>
/// <param name="dictTypeCode"></param>
/// <param name="errorMessage"></param>
public DictAttribute(string dictTypeCode = "", string errorMessage = "字典值不合法!")
/// <param name="dictTypeCode">字典类型编码</param>
/// <param name="multiple">是否允许多选</param>
/// <param name="errorMessage">自定义错误消息</param>
public DictAttribute(string dictTypeCode = "", bool multiple = false, string errorMessage = "字典值不合法!")
{
DictTypeCode = dictTypeCode;
Multiple = multiple;
ErrorMessage = errorMessage;
}
/// <summary>
/// 字典值合规性校
/// 证方法(异步安全)
/// </summary>
/// <param name="value"></param>
/// <param name="validationContext"></param>
/// <returns></returns>
protected override ValidationResult IsValid(object? value, ValidationContext validationContext)
private async Task<ValidationResult> IsValidAsync(object? value, ValidationContext validationContext, CancellationToken cancellation)
{
// 判断是否允许空值
if (AllowNullValue && value == null) return ValidationResult.Success;
// 1. 空值检查
if (IsEmpty(value))
return ValidationResult.Success;
// 获取属性类型
// 2. 获取属性类型
var property = validationContext.ObjectType.GetProperty(validationContext.MemberName!);
if (property == null) return new ValidationResult($"未知属性: {validationContext.MemberName}");
string importHeaderName = GetImporterHeaderName(property, validationContext.MemberName);
if (property == null)
return new ValidationResult($"未知属性: {validationContext.MemberName}");
var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
// 先尝试从 ValidationContext 的依赖注入容器中拿服务,拿不到或类型不匹配时,再从全局的 App 容器中获取
if (validationContext.GetService(typeof(SysDictDataService)) is not SysDictDataService sysDictDataService)
sysDictDataService = App.GetRequiredService<SysDictDataService>();
// 获取字典值列表
var dictDataList = sysDictDataService.GetDataList(DictTypeCode).GetAwaiter().GetResult();
// 使用 HashSet 来提高查找效率
var dictHash = new HashSet<string>(dictDataList.Select(u => u.Value));
// 判断是否为集合类型
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>))
{
// 如果是空集合并且允许空值,则直接返回成功
if (value == null && AllowNullValue) return ValidationResult.Success;
// 处理集合为空的情况
if (value is not IEnumerable collection) return ValidationResult.Success;
// 获取集合的元素类型
var elementType = propertyType.GetGenericArguments()[0];
var underlyingElementType = Nullable.GetUnderlyingType(elementType) ?? elementType;
// 如果元素类型是枚举,则逐个验证
if (underlyingElementType.IsEnum)
{
foreach (var item in collection)
{
if (item == null && AllowNullValue) continue;
if (!Enum.IsDefined(underlyingElementType, item!))
return new ValidationResult($"提示:{ErrorMessage}|枚举值【{item}】不是有效的【{underlyingElementType.Name}】枚举类型值!", [importHeaderName]);
}
return ValidationResult.Success;
}
foreach (var item in collection)
{
if (item == null && AllowNullValue) continue;
var itemString = item?.ToString();
if (!dictHash.Contains(itemString))
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{itemString}】!", [importHeaderName]);
}
return ValidationResult.Success;
}
var valueAsString = value?.ToString();
// 是否忽略空字符串
if (AllowEmptyStrings && string.IsNullOrEmpty(valueAsString)) return ValidationResult.Success;
// 枚举类型验证
// 3. 枚举类型校验
if (propertyType.IsEnum)
{
if (!Enum.IsDefined(propertyType, value!)) return new ValidationResult($"提示:{ErrorMessage}|枚举值【{value}】不是有效的【{propertyType.Name}】枚举类型值!", [importHeaderName]);
return !Enum.IsDefined(propertyType, value!) ? new ValidationResult($"提示:{ErrorMessage}|枚举值【{value}】不是有效的【{propertyType.Name}】枚举类型值!") : ValidationResult.Success;
}
// 4. 获取字典服务
var sysDictDataService = validationContext.GetService(typeof(SysDictDataService)) as SysDictDataService
?? App.GetRequiredService<SysDictDataService>();
// 5. 异步获取字典数据
var dictDataList = await sysDictDataService.GetDataList(DictTypeCode);
var dictHashSet = new HashSet<string>(dictDataList.Select(d => d.Code), StringComparer.OrdinalIgnoreCase);
// 6. 单选校验
if (!Multiple)
{
var valStr = value?.ToString()?.Trim();
if (string.IsNullOrEmpty(valStr) || !dictHashSet.Contains(valStr))
{
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{value}】!");
}
return ValidationResult.Success;
}
if (!dictHash.Contains(valueAsString))
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{valueAsString}】!", [importHeaderName]);
// 7. 多选校验
if (value is string stringValue)
{
var codes = stringValue.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
foreach (var code in codes)
{
if (!dictHashSet.Contains(code))
{
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{code}】!");
}
}
return ValidationResult.Success;
}
return ValidationResult.Success;
// 集合类数据校验
if (value is IList listValue)
{
foreach (var item in listValue)
{
var itemStr = item?.ToString()?.Trim();
if (string.IsNullOrEmpty(itemStr) || !dictHashSet.Contains(itemStr))
{
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{item}】!");
}
}
return ValidationResult.Success;
}
// 8. 类型不匹配
return new ValidationResult($"提示:{ErrorMessage}|不支持的值类型:{value?.GetType().Name},多选时应为逗号分隔字符串或集合类型。");
}
/// <summary>
/// 获取本字段上 [ImporterHeader(Name = "xxx")] 里的Name如果没有则使用defaultName.
/// 用于在从excel导入数据时能让调用者知道是哪个字段验证失败而不是抛异常
/// 同步入口(兼容旧版调用)
/// </summary>
private static string GetImporterHeaderName(PropertyInfo property, string defaultName)
protected override ValidationResult IsValid(object? value, ValidationContext validationContext)
{
var importerHeader = property.GetCustomAttribute<ImporterHeaderAttribute>();
string importerHeaderName = importerHeader?.Name ?? defaultName;
return importerHeaderName;
return IsValidAsync(value, validationContext, CancellationToken.None).GetAwaiter().GetResult();
}
/// <summary>
/// 判断值是否为空null、空字符串、空集合
/// </summary>
private static bool IsEmpty(object? value)
{
return value switch
{
null => true,
string s => string.IsNullOrWhiteSpace(s),
IList list => list.Count == 0,
_ => false
};
}
}