😎优化枚举验证特性

This commit is contained in:
zuohuaijun 2025-01-12 15:33:49 +08:00
parent 47526853ca
commit 141a270bcf
3 changed files with 9 additions and 6 deletions

View File

@ -45,7 +45,7 @@
<PackageReference Include="SixLabors.ImageSharp.Web" Version="3.1.3" />
<PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="3.6.0" />
<PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="3.9.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.172" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.173" />
<PackageReference Include="SSH.NET" Version="2024.2.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.5.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />

View File

@ -35,14 +35,17 @@ public class EnumAttribute : ValidationAttribute, ITransient
if (property == null)
return new ValidationResult($"未知属性: {validationContext.MemberName}");
var propertyType = property.PropertyType;
var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
// 检查属性类型是否为枚举
// 检查属性类型是否为枚举或可空枚举类型
if (!propertyType.IsEnum)
return new ValidationResult($"属性类型'{validationContext.MemberName}'不是有效的枚举类型!");
// 检查枚举值是否有效
if (!Enum.IsDefined(propertyType, value))
if (value == null && Nullable.GetUnderlyingType(property.PropertyType) == null)
return new ValidationResult($"提示:{ErrorMessage}|枚举值不能为 null");
if (value != null && !Enum.IsDefined(propertyType, value))
return new ValidationResult($"提示:{ErrorMessage}|枚举值【{value}】不是有效的【{propertyType.Name}】枚举类型值!");
return ValidationResult.Success;

View File

@ -60,7 +60,7 @@
"screenfull": "^6.0.2",
"sm-crypto-v2": "^1.9.3",
"sortablejs": "^1.15.6",
"splitpanes": "^3.1.6",
"splitpanes": "^3.1.8",
"vcrontab-3": "^3.3.22",
"vform3-builds": "^3.0.10",
"vue": "^3.5.13",
@ -85,7 +85,7 @@
"@iconify/vue": "^4.3.0",
"@plugin-web-update-notification/vite": "^1.7.1",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.17.11",
"@types/node": "^20.17.12",
"@types/nprogress": "^0.2.3",
"@types/sortablejs": "^1.15.8",
"@typescript-eslint/eslint-plugin": "^8.19.1",