🍒 fix(条件必填): 修复已知的校验bug
This commit is contained in:
parent
85af7edee2
commit
a17fb5f182
@ -68,14 +68,9 @@ public sealed class RequiredIFAttribute(
|
||||
if (targetProperty == null) return new ValidationResult($"找不到属性: {PropertyName}");
|
||||
var targetValue = targetProperty.GetValue(instance);
|
||||
|
||||
if (!ShouldValidate(targetValue))
|
||||
{
|
||||
return ValidationResult.Success;
|
||||
}
|
||||
if (!ShouldValidate(targetValue)) return ValidationResult.Success;
|
||||
|
||||
return IsEmpty(value)
|
||||
? new ValidationResult(ErrorMessage ?? $"{validationContext.MemberName}不能为空")
|
||||
: ValidationResult.Success;
|
||||
return IsEmpty(value) ? new ValidationResult(ErrorMessage ?? $"{validationContext.MemberName}不能为空") : ValidationResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -85,11 +80,65 @@ public sealed class RequiredIFAttribute(
|
||||
/// <returns>是否需要验证</returns>
|
||||
private bool ShouldValidate(object targetValue)
|
||||
{
|
||||
if (TargetValue == null) return IsEmpty(targetValue);
|
||||
switch (Comparison)
|
||||
{
|
||||
case Operator.Equal:
|
||||
return TargetValue == null ? IsEmpty(targetValue) : CompareValues(targetValue, TargetValue, Comparison);
|
||||
case Operator.NotEqual:
|
||||
return TargetValue == null ? !IsEmpty(targetValue) : CompareValues(targetValue, TargetValue, Comparison);
|
||||
case Operator.GreaterThan:
|
||||
case Operator.LessThan:
|
||||
case Operator.GreaterThanOrEqual:
|
||||
case Operator.LessThanOrEqual:
|
||||
case Operator.Contains:
|
||||
case Operator.NotContains:
|
||||
if (targetValue is IEnumerable enumerable) return enumerable.Cast<object>().Any(item => CompareValues(item, TargetValue, Comparison));
|
||||
return TargetValue == null ? !IsEmpty(targetValue) : CompareValues(targetValue, TargetValue, Comparison);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return TargetValue is IEnumerable enumerable and not string
|
||||
? enumerable.Cast<object>().Any(item => CompareValues(targetValue, item, Operator.Equal))
|
||||
: CompareValues(targetValue, TargetValue, Comparison);
|
||||
/// <summary>
|
||||
/// 比较两个值
|
||||
/// </summary>
|
||||
/// <param name="sourceValue">源值</param>
|
||||
/// <param name="targetValue">目标值</param>
|
||||
/// <param name="comparison">比较运算符</param>
|
||||
/// <returns>比较结果</returns>
|
||||
private static bool CompareValues(object sourceValue, object targetValue, Operator comparison)
|
||||
{
|
||||
switch (comparison)
|
||||
{
|
||||
case Operator.Equal:
|
||||
case Operator.NotEqual:
|
||||
case Operator.GreaterThan:
|
||||
case Operator.LessThan:
|
||||
case Operator.GreaterThanOrEqual:
|
||||
case Operator.LessThanOrEqual:
|
||||
if (sourceValue is IComparable sourceComparable && targetValue is IComparable targetComparable)
|
||||
{
|
||||
int result = sourceComparable.CompareTo(targetComparable);
|
||||
return comparison switch
|
||||
{
|
||||
Operator.Equal => result == 0,
|
||||
Operator.NotEqual => result != 0,
|
||||
Operator.GreaterThan => result > 0,
|
||||
Operator.LessThan => result < 0,
|
||||
Operator.GreaterThanOrEqual => result >= 0,
|
||||
Operator.LessThanOrEqual => result <= 0,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
return false;
|
||||
case Operator.Contains:
|
||||
case Operator.NotContains:
|
||||
if (targetValue is not IEnumerable enumerable) return false;
|
||||
bool contains = enumerable.Cast<object>().Any(item => item != null && item.Equals(sourceValue));
|
||||
return comparison == Operator.Contains ? contains : !contains;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -106,59 +155,6 @@ public sealed class RequiredIFAttribute(
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 比较两个值
|
||||
/// </summary>
|
||||
/// <param name="sourceValue">源值</param>
|
||||
/// <param name="targetValue">目标值</param>
|
||||
/// <param name="comparison">比较运算符</param>
|
||||
/// <returns>比较结果</returns>
|
||||
private static bool CompareValues(object sourceValue, object targetValue, Operator comparison)
|
||||
{
|
||||
// 处理null值比较
|
||||
if (sourceValue == null || targetValue == null)
|
||||
{
|
||||
return comparison switch
|
||||
{
|
||||
Operator.Equal => sourceValue == targetValue,
|
||||
Operator.NotEqual => sourceValue != targetValue,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
// 处理集合包含操作
|
||||
if (comparison is Operator.Contains or Operator.NotContains)
|
||||
{
|
||||
if (targetValue is not IEnumerable enumerable)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool contains = enumerable.Cast<object>().Any(item => item != null && item.Equals(sourceValue));
|
||||
return comparison == Operator.Contains ? contains : !contains;
|
||||
}
|
||||
|
||||
// 处理可比较类型
|
||||
if (sourceValue is IComparable sourceComparable && targetValue is IComparable targetComparable)
|
||||
{
|
||||
int result = sourceComparable.CompareTo(targetComparable);
|
||||
return comparison switch
|
||||
{
|
||||
Operator.Equal => result == 0,
|
||||
Operator.NotEqual => result != 0,
|
||||
Operator.GreaterThan => result > 0,
|
||||
Operator.LessThan => result < 0,
|
||||
Operator.GreaterThanOrEqual => result >= 0,
|
||||
Operator.LessThanOrEqual => result <= 0,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
// 默认比较
|
||||
bool equals = sourceValue.Equals(targetValue);
|
||||
return comparison == Operator.Equal ? equals : !equals;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user