🍒 refactor(Core): 代码简化
This commit is contained in:
parent
67b8c7f0e4
commit
a2da22c037
@ -31,29 +31,26 @@ namespace Admin.NET.Core;
|
||||
/// public string SomeProperty { get; set; } <br/>
|
||||
/// </code>
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public sealed class RequiredIFAttribute : ValidationAttribute
|
||||
public sealed class RequiredIFAttribute(
|
||||
string propertyName,
|
||||
object targetValue = null,
|
||||
Operator comparison = Operator.Equal)
|
||||
: ValidationAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 依赖的属性名称
|
||||
/// </summary>
|
||||
private string PropertyName { get; set; }
|
||||
private string PropertyName { get; set; } = propertyName;
|
||||
|
||||
/// <summary>
|
||||
/// 目标比较值
|
||||
/// </summary>
|
||||
private object TargetValue { get; set; }
|
||||
private object TargetValue { get; set; } = targetValue;
|
||||
|
||||
/// <summary>
|
||||
/// 比较运算符
|
||||
/// </summary>
|
||||
private Operator Comparison { get; set; }
|
||||
|
||||
public RequiredIFAttribute(string propertyName, object targetValue = null, Operator comparison = Operator.Equal)
|
||||
{
|
||||
PropertyName = propertyName;
|
||||
TargetValue = targetValue;
|
||||
Comparison = comparison;
|
||||
}
|
||||
private Operator Comparison { get; set; } = comparison;
|
||||
|
||||
/// <summary>
|
||||
/// 验证属性值是否符合要求
|
||||
@ -76,7 +73,7 @@ public sealed class RequiredIFAttribute : ValidationAttribute
|
||||
return ValidationResult.Success;
|
||||
}
|
||||
|
||||
return IsValueEmpty(value)
|
||||
return IsEmpty(value)
|
||||
? new ValidationResult(ErrorMessage ?? $"{validationContext.MemberName}不能为空")
|
||||
: ValidationResult.Success;
|
||||
}
|
||||
@ -88,7 +85,7 @@ public sealed class RequiredIFAttribute : ValidationAttribute
|
||||
/// <returns>是否需要验证</returns>
|
||||
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
|
||||
? enumerable.Cast<object>().Any(item => CompareValues(targetValue, item, Operator.Equal))
|
||||
@ -98,11 +95,16 @@ public sealed class RequiredIFAttribute : ValidationAttribute
|
||||
/// <summary>
|
||||
/// 检查值是否为空
|
||||
/// </summary>
|
||||
/// <param name="value">要检查的值</param>
|
||||
/// <returns>值是否为空</returns>
|
||||
private static bool IsValueEmpty(object value)
|
||||
private static bool IsEmpty(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>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user