// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core;
///
/// 条件必填参数验证特性(支持多条件判断)
/// 当另一个属性的值满足指定条件时,验证当前属性是否非空。
///
///
/// // 实例1
/// [RequiredIF(nameof(TarProperty), 1, ErrorMessage = "TarProperty为1时,SomeProperty不能为空")]
/// public string SomeProperty { get; set; }
///
///
/// // 实例2
/// [RequiredIF(nameof(TarProperty), 1, Operator.NotEqual, ErrorMessage = "TarProperty不为1时,SomeProperty不能为空")]
/// public string SomeProperty { get; set; }
///
///
/// // 实例3
/// [RequiredIF(nameof(TarProperty), new[]{ 1, 2 }, Operator.Contains, ErrorMessage = "TarProperty包含为1或2时,SomeProperty不能为空")]
/// public string SomeProperty { get; set; }
///
///
/// // 实例4
/// [RequiredIF(nameof(TarProperty), new[]{ 1, 2 }, Operator.NotContains, ErrorMessage = "TarProperty不包含1和2时,SomeProperty不能为空")]
/// public string SomeProperty { get; set; }
///
[AttributeUsage(AttributeTargets.Property)]
public sealed class RequiredIFAttribute(
string propertyName,
object targetValue = null,
Operator comparison = Operator.Equal)
: ValidationAttribute
{
///
/// 依赖的属性名称
///
private string PropertyName { get; set; } = propertyName;
///
/// 目标比较值
///
private object TargetValue { get; set; } = targetValue;
///
/// 比较运算符
///
private Operator Comparison { get; set; } = comparison;
///
/// 验证属性值是否符合要求
///
/// 当前属性的值
/// 验证上下文
/// 验证结果
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
ArgumentNullException.ThrowIfNull(validationContext);
var instance = validationContext.ObjectInstance;
var targetProperty = instance.GetType().GetProperty(PropertyName);
if (targetProperty == null) return new ValidationResult($"找不到属性: {PropertyName}");
var targetValue = targetProperty.GetValue(instance);
if (!ShouldValidate(targetValue))
{
return ValidationResult.Success;
}
return IsEmpty(value)
? new ValidationResult(ErrorMessage ?? $"{validationContext.MemberName}不能为空")
: ValidationResult.Success;
}
///
/// 判断是否需要进行验证
///
/// 依赖属性的值
/// 是否需要验证
private bool ShouldValidate(object targetValue)
{
if (TargetValue == null) return IsEmpty(targetValue);
return TargetValue is IEnumerable enumerable and not string
? enumerable.Cast