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]
|
2024-06-23 17:03:52 +08:00
|
|
|
|
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
|
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>
|
|
|
|
|
|
public string DictTypeCode { get; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否允许空字符串
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool AllowEmptyStrings { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 允许空值,有值才验证,默认 false
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool AllowNullValue { get; set; } = false;
|
|
|
|
|
|
|
2024-06-23 17:03:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 字典值合规性校验特性
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dictTypeCode"></param>
|
|
|
|
|
|
/// <param name="errorMessage"></param>
|
2025-01-12 22:11:41 +08:00
|
|
|
|
public DictAttribute(string dictTypeCode = "", string errorMessage = "字典值不合法!")
|
2024-06-23 17:03:52 +08:00
|
|
|
|
{
|
|
|
|
|
|
DictTypeCode = dictTypeCode;
|
|
|
|
|
|
ErrorMessage = errorMessage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 字典值合规性校验
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <param name="validationContext"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected override ValidationResult IsValid(object? value, ValidationContext validationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 判断是否允许空值
|
2025-01-14 14:47:34 +08:00
|
|
|
|
if (AllowNullValue && value == null) return ValidationResult.Success;
|
2024-06-23 17:03:52 +08:00
|
|
|
|
|
2025-01-12 22:11:41 +08:00
|
|
|
|
var valueAsString = value?.ToString();
|
2024-06-23 17:03:52 +08:00
|
|
|
|
|
2025-01-12 22:11:41 +08:00
|
|
|
|
// 是否忽略空字符串
|
2025-01-14 14:47:34 +08:00
|
|
|
|
if (AllowEmptyStrings && string.IsNullOrEmpty(valueAsString)) return ValidationResult.Success;
|
2024-06-23 17:03:52 +08:00
|
|
|
|
|
2025-01-12 22:11:41 +08:00
|
|
|
|
// 获取属性的类型
|
2025-01-14 14:47:34 +08:00
|
|
|
|
var property = validationContext.ObjectType.GetProperty(validationContext.MemberName!);
|
|
|
|
|
|
if (property == null) return new ValidationResult($"未知属性: {validationContext.MemberName}");
|
2024-12-03 12:27:29 +08:00
|
|
|
|
|
2025-01-12 22:11:41 +08:00
|
|
|
|
var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
|
2025-01-14 14:47:34 +08:00
|
|
|
|
|
2025-01-12 22:11:41 +08:00
|
|
|
|
// 枚举类型验证
|
|
|
|
|
|
if (propertyType.IsEnum)
|
|
|
|
|
|
{
|
2025-01-14 14:47:34 +08:00
|
|
|
|
if (!Enum.IsDefined(propertyType, value!)) return new ValidationResult($"提示:{ErrorMessage}|枚举值【{value}】不是有效的【{propertyType.Name}】枚举类型值!");
|
|
|
|
|
|
return ValidationResult.Success;
|
2025-01-12 22:11:41 +08:00
|
|
|
|
}
|
2024-06-23 17:03:52 +08:00
|
|
|
|
|
2025-01-15 01:03:08 +08:00
|
|
|
|
// 先尝试从 ValidationContext 的依赖注入容器中拿服务,拿不到或类型不匹配时,再从全局的 App 容器中获取
|
|
|
|
|
|
if (validationContext.GetService(typeof(SysDictTypeService)) is not SysDictTypeService sysDictDataService)
|
|
|
|
|
|
sysDictDataService = App.GetRequiredService<SysDictTypeService>();
|
|
|
|
|
|
|
2025-01-12 22:11:41 +08:00
|
|
|
|
// 获取字典值列表
|
2025-01-15 01:03:08 +08:00
|
|
|
|
var dictDataList = sysDictDataService.GetDataList(new GetDataDictTypeInput { Code = DictTypeCode }).GetAwaiter().GetResult();
|
|
|
|
|
|
|
2025-01-12 22:11:41 +08:00
|
|
|
|
// 使用 HashSet 来提高查找效率
|
|
|
|
|
|
var dictHash = new HashSet<string>(dictDataList.Select(u => u.Code));
|
2025-01-15 01:03:08 +08:00
|
|
|
|
|
2025-01-14 14:47:34 +08:00
|
|
|
|
if (!dictHash.Contains(valueAsString)) return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{valueAsString}】!");
|
2025-01-12 22:11:41 +08:00
|
|
|
|
|
2024-11-25 16:27:14 +08:00
|
|
|
|
return ValidationResult.Success;
|
2024-06-23 17:03:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|