|
|
@@ -1,4 +1,4 @@
|
|
|
-// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|
|
+// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|
|
//
|
|
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|
|
//
|
|
|
@@ -50,24 +50,12 @@ public class DictAttribute : ValidationAttribute, ITransient
|
|
|
// 判断是否允许空值
|
|
|
if (AllowNullValue && value == null) return ValidationResult.Success;
|
|
|
|
|
|
- var valueAsString = value?.ToString();
|
|
|
-
|
|
|
- // 是否忽略空字符串
|
|
|
- if (AllowEmptyStrings && string.IsNullOrEmpty(valueAsString)) return ValidationResult.Success;
|
|
|
-
|
|
|
// 获取属性的类型
|
|
|
var property = validationContext.ObjectType.GetProperty(validationContext.MemberName!);
|
|
|
if (property == null) return new ValidationResult($"未知属性: {validationContext.MemberName}");
|
|
|
|
|
|
var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
|
|
|
|
|
|
- // 枚举类型验证
|
|
|
- if (propertyType.IsEnum)
|
|
|
- {
|
|
|
- if (!Enum.IsDefined(propertyType, value!)) return new ValidationResult($"提示:{ErrorMessage}|枚举值【{value}】不是有效的【{propertyType.Name}】枚举类型值!");
|
|
|
- return ValidationResult.Success;
|
|
|
- }
|
|
|
-
|
|
|
// 先尝试从 ValidationContext 的依赖注入容器中拿服务,拿不到或类型不匹配时,再从全局的 App 容器中获取
|
|
|
if (validationContext.GetService(typeof(SysDictDataService)) is not SysDictDataService sysDictDataService)
|
|
|
sysDictDataService = App.GetRequiredService<SysDictDataService>();
|
|
|
@@ -78,6 +66,57 @@ public class DictAttribute : ValidationAttribute, ITransient
|
|
|
// 使用 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;
|
|
|
+
|
|
|
+ // 处理集合为空的情况
|
|
|
+ var collection = value as IEnumerable;
|
|
|
+ if (collection == null) 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}】枚举类型值!");
|
|
|
+ }
|
|
|
+ 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}】!");
|
|
|
+ }
|
|
|
+
|
|
|
+ return ValidationResult.Success;
|
|
|
+ }
|
|
|
+
|
|
|
+ var valueAsString = value?.ToString();
|
|
|
+
|
|
|
+ // 是否忽略空字符串
|
|
|
+ if (AllowEmptyStrings && string.IsNullOrEmpty(valueAsString)) return ValidationResult.Success;
|
|
|
+
|
|
|
+ // 枚举类型验证
|
|
|
+ if (propertyType.IsEnum)
|
|
|
+ {
|
|
|
+ if (!Enum.IsDefined(propertyType, value!)) return new ValidationResult($"提示:{ErrorMessage}|枚举值【{value}】不是有效的【{propertyType.Name}】枚举类型值!");
|
|
|
+ return ValidationResult.Success;
|
|
|
+ }
|
|
|
+
|
|
|
if (!dictHash.Contains(valueAsString)) return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{valueAsString}】!");
|
|
|
|
|
|
return ValidationResult.Success;
|