2024-06-23 17:03:52 +08:00
|
|
|
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
|
|
|
|
|
|
|
|
|
|
|
namespace Admin.NET.Core;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 字典值合规性校验特性
|
|
|
|
|
|
/// </summary>
|
2025-01-12 22:11:41 +08:00
|
|
|
|
[SuppressSniffer]
|
2025-08-17 12:45:36 +08:00
|
|
|
|
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
|
2025-01-12 22:11:41 +08:00
|
|
|
|
public class DictAttribute : ValidationAttribute, ITransient
|
2024-06-23 17:03:52 +08:00
|
|
|
|
{
|
2024-12-03 12:27:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 字典编码
|
|
|
|
|
|
/// </summary>
|
2025-08-17 12:45:36 +08:00
|
|
|
|
public readonly string DictTypeCode;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否多选
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public readonly bool Multiple;
|
2024-12-03 12:27:29 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否允许空字符串
|
|
|
|
|
|
/// </summary>
|
2025-08-17 12:49:53 +08:00
|
|
|
|
[Obsolete("参数已废弃,逻辑已删除,请使用[Required]特性校验必填")]
|
2024-12-03 12:27:29 +08:00
|
|
|
|
public bool AllowEmptyStrings { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 允许空值,有值才验证,默认 false
|
|
|
|
|
|
/// </summary>
|
2025-08-17 12:49:53 +08:00
|
|
|
|
[Obsolete("参数已废弃,逻辑已删除,请使用[Required]特性校验必填")]
|
2024-12-03 12:27:29 +08:00
|
|
|
|
public bool AllowNullValue { get; set; } = false;
|
|
|
|
|
|
|
2024-06-23 17:03:52 +08:00
|
|
|
|
/// <summary>
|
2025-08-17 12:45:36 +08:00
|
|
|
|
/// 构造函数
|
2024-06-23 17:03:52 +08:00
|
|
|
|
/// </summary>
|
2025-08-17 12:45:36 +08:00
|
|
|
|
/// <param name="dictTypeCode">字典类型编码</param>
|
|
|
|
|
|
/// <param name="multiple">是否允许多选</param>
|
|
|
|
|
|
/// <param name="errorMessage">自定义错误消息</param>
|
|
|
|
|
|
public DictAttribute(string dictTypeCode = "", bool multiple = false, string errorMessage = "字典值不合法!")
|
2024-06-23 17:03:52 +08:00
|
|
|
|
{
|
|
|
|
|
|
DictTypeCode = dictTypeCode;
|
2025-08-17 12:45:36 +08:00
|
|
|
|
Multiple = multiple;
|
2024-06-23 17:03:52 +08:00
|
|
|
|
ErrorMessage = errorMessage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-08-17 12:45:36 +08:00
|
|
|
|
/// 验证方法(异步安全)
|
2024-06-23 17:03:52 +08:00
|
|
|
|
/// </summary>
|
2025-08-17 12:45:36 +08:00
|
|
|
|
private async Task<ValidationResult> IsValidAsync(object? value, ValidationContext validationContext, CancellationToken cancellation)
|
2024-06-23 17:03:52 +08:00
|
|
|
|
{
|
2025-08-17 12:45:36 +08:00
|
|
|
|
// 1. 空值检查
|
2025-08-31 21:34:41 +08:00
|
|
|
|
if (IsEmpty(value)) return ValidationResult.Success;
|
2024-06-23 17:03:52 +08:00
|
|
|
|
|
2025-08-17 12:45:36 +08:00
|
|
|
|
// 2. 获取属性类型
|
2025-01-14 14:47:34 +08:00
|
|
|
|
var property = validationContext.ObjectType.GetProperty(validationContext.MemberName!);
|
2025-08-17 12:45:36 +08:00
|
|
|
|
if (property == null)
|
|
|
|
|
|
return new ValidationResult($"未知属性: {validationContext.MemberName}");
|
2025-01-14 14:47:34 +08:00
|
|
|
|
|
2025-06-11 01:18:59 +08:00
|
|
|
|
var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
|
2024-06-23 17:03:52 +08:00
|
|
|
|
|
2025-08-17 12:45:36 +08:00
|
|
|
|
// 3. 枚举类型校验
|
|
|
|
|
|
if (propertyType.IsEnum)
|
2025-06-11 01:18:59 +08:00
|
|
|
|
{
|
2025-08-17 12:45:36 +08:00
|
|
|
|
return !Enum.IsDefined(propertyType, value!) ? new ValidationResult($"提示:{ErrorMessage}|枚举值【{value}】不是有效的【{propertyType.Name}】枚举类型值!") : ValidationResult.Success;
|
|
|
|
|
|
}
|
2025-06-11 01:18:59 +08:00
|
|
|
|
|
2025-08-17 12:45:36 +08:00
|
|
|
|
// 4. 获取字典服务
|
|
|
|
|
|
var sysDictDataService = validationContext.GetService(typeof(SysDictDataService)) as SysDictDataService
|
|
|
|
|
|
?? App.GetRequiredService<SysDictDataService>();
|
2025-06-11 01:18:59 +08:00
|
|
|
|
|
2025-08-17 12:45:36 +08:00
|
|
|
|
// 5. 异步获取字典数据
|
|
|
|
|
|
var dictDataList = await sysDictDataService.GetDataList(DictTypeCode);
|
2025-08-18 11:07:00 +08:00
|
|
|
|
var dictHashSet = new HashSet<string>(dictDataList.Select(d => d.Value), StringComparer.OrdinalIgnoreCase);
|
2025-06-11 01:18:59 +08:00
|
|
|
|
|
2025-08-17 12:45:36 +08:00
|
|
|
|
// 6. 单选校验
|
|
|
|
|
|
if (!Multiple)
|
|
|
|
|
|
{
|
2025-08-31 21:34:41 +08:00
|
|
|
|
var valStr = value!.GetType().IsEnum ? value.ToInt().ToString() : value.ToString();
|
2025-08-17 12:45:36 +08:00
|
|
|
|
if (string.IsNullOrEmpty(valStr) || !dictHashSet.Contains(valStr))
|
2025-06-11 01:18:59 +08:00
|
|
|
|
{
|
2025-08-17 12:45:36 +08:00
|
|
|
|
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{value}】!");
|
2025-06-11 01:18:59 +08:00
|
|
|
|
}
|
2025-08-17 12:45:36 +08:00
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
|
}
|
2025-06-11 01:18:59 +08:00
|
|
|
|
|
2025-08-17 12:45:36 +08:00
|
|
|
|
// 7. 多选校验
|
|
|
|
|
|
if (value is string stringValue)
|
|
|
|
|
|
{
|
2025-08-17 19:13:20 +08:00
|
|
|
|
var codes = stringValue.Split(',');
|
2025-08-17 12:45:36 +08:00
|
|
|
|
foreach (var code in codes)
|
2025-06-11 01:18:59 +08:00
|
|
|
|
{
|
2025-08-17 12:45:36 +08:00
|
|
|
|
if (!dictHashSet.Contains(code))
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{code}】!");
|
|
|
|
|
|
}
|
2025-06-11 01:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-17 12:45:36 +08:00
|
|
|
|
// 集合类数据校验
|
|
|
|
|
|
if (value is IList listValue)
|
2025-06-11 01:18:59 +08:00
|
|
|
|
{
|
2025-08-17 12:45:36 +08:00
|
|
|
|
foreach (var item in listValue)
|
|
|
|
|
|
{
|
2025-08-31 21:34:41 +08:00
|
|
|
|
var itemStr = item.GetType().IsEnum ? item.ToInt().ToString() : item.ToString();
|
2025-08-17 12:45:36 +08:00
|
|
|
|
if (string.IsNullOrEmpty(itemStr) || !dictHashSet.Contains(itemStr))
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{item}】!");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-11 01:18:59 +08:00
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-17 12:45:36 +08:00
|
|
|
|
// 8. 类型不匹配
|
|
|
|
|
|
return new ValidationResult($"提示:{ErrorMessage}|不支持的值类型:{value?.GetType().Name},多选时应为逗号分隔字符串或集合类型。");
|
|
|
|
|
|
}
|
2025-01-12 22:11:41 +08:00
|
|
|
|
|
2025-08-17 12:45:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 同步入口(兼容旧版调用)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected override ValidationResult IsValid(object? value, ValidationContext validationContext)
|
|
|
|
|
|
{
|
2025-09-14 16:30:13 +08:00
|
|
|
|
return IsValidAsync(value, validationContext, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
|
2024-06-23 17:03:52 +08:00
|
|
|
|
}
|
2025-06-11 01:18:59 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-08-17 12:45:36 +08:00
|
|
|
|
/// 判断值是否为空(null、空字符串、空集合)
|
2025-06-11 01:18:59 +08:00
|
|
|
|
/// </summary>
|
2025-08-17 12:45:36 +08:00
|
|
|
|
private static bool IsEmpty(object? value)
|
2025-06-11 01:18:59 +08:00
|
|
|
|
{
|
2025-08-17 12:45:36 +08:00
|
|
|
|
return value switch
|
|
|
|
|
|
{
|
|
|
|
|
|
null => true,
|
|
|
|
|
|
string s => string.IsNullOrWhiteSpace(s),
|
|
|
|
|
|
IList list => list.Count == 0,
|
|
|
|
|
|
_ => false
|
|
|
|
|
|
};
|
2025-06-11 01:18:59 +08:00
|
|
|
|
}
|
2024-06-23 17:03:52 +08:00
|
|
|
|
}
|