🍒 refactor(Core): 代码简化

This commit is contained in:
喵你个汪呀 2025-08-25 15:59:50 +08:00
parent 67b8c7f0e4
commit a2da22c037

View File

@ -31,29 +31,26 @@ namespace Admin.NET.Core;
/// public string SomeProperty { get; set; } <br/> /// public string SomeProperty { get; set; } <br/>
/// </code> /// </code>
[AttributeUsage(AttributeTargets.Property)] [AttributeUsage(AttributeTargets.Property)]
public sealed class RequiredIFAttribute : ValidationAttribute public sealed class RequiredIFAttribute(
string propertyName,
object targetValue = null,
Operator comparison = Operator.Equal)
: ValidationAttribute
{ {
/// <summary> /// <summary>
/// 依赖的属性名称 /// 依赖的属性名称
/// </summary> /// </summary>
private string PropertyName { get; set; } private string PropertyName { get; set; } = propertyName;
/// <summary> /// <summary>
/// 目标比较值 /// 目标比较值
/// </summary> /// </summary>
private object TargetValue { get; set; } private object TargetValue { get; set; } = targetValue;
/// <summary> /// <summary>
/// 比较运算符 /// 比较运算符
/// </summary> /// </summary>
private Operator Comparison { get; set; } private Operator Comparison { get; set; } = comparison;
public RequiredIFAttribute(string propertyName, object targetValue = null, Operator comparison = Operator.Equal)
{
PropertyName = propertyName;
TargetValue = targetValue;
Comparison = comparison;
}
/// <summary> /// <summary>
/// 验证属性值是否符合要求 /// 验证属性值是否符合要求
@ -76,7 +73,7 @@ public sealed class RequiredIFAttribute : ValidationAttribute
return ValidationResult.Success; return ValidationResult.Success;
} }
return IsValueEmpty(value) return IsEmpty(value)
? new ValidationResult(ErrorMessage ?? $"{validationContext.MemberName}不能为空") ? new ValidationResult(ErrorMessage ?? $"{validationContext.MemberName}不能为空")
: ValidationResult.Success; : ValidationResult.Success;
} }
@ -88,7 +85,7 @@ public sealed class RequiredIFAttribute : ValidationAttribute
/// <returns>是否需要验证</returns> /// <returns>是否需要验证</returns>
private bool ShouldValidate(object targetValue) private bool ShouldValidate(object targetValue)
{ {
if (TargetValue == null) return targetValue == null || (targetValue is string str && string.IsNullOrWhiteSpace(str)) || targetValue is long and 0; if (TargetValue == null) return IsEmpty(targetValue);
return TargetValue is IEnumerable enumerable and not string return TargetValue is IEnumerable enumerable and not string
? enumerable.Cast<object>().Any(item => CompareValues(targetValue, item, Operator.Equal)) ? enumerable.Cast<object>().Any(item => CompareValues(targetValue, item, Operator.Equal))
@ -98,11 +95,16 @@ public sealed class RequiredIFAttribute : ValidationAttribute
/// <summary> /// <summary>
/// 检查值是否为空 /// 检查值是否为空
/// </summary> /// </summary>
/// <param name="value">要检查的值</param> private static bool IsEmpty(object? value)
/// <returns>值是否为空</returns>
private static bool IsValueEmpty(object value)
{ {
return value == null || (value is string str && string.IsNullOrWhiteSpace(str)) || value is long and 0; return value switch
{
null => true,
long l => l == 0,
string s => string.IsNullOrWhiteSpace(s),
IList list => list.Count == 0,
_ => false
};
} }
/// <summary> /// <summary>