简体   繁体   English

WebAPI-FluentValidation-根据父模型值验证子模型属性

[英]WebAPI - FluentValidation - Validate Child model properties based on parent model value

public class ParentClass
{
    public int ParentId { get; set; }
    public int ActivityId { get; set; }
    public DateTime UpateDate { get; set; }
    public IList<ChildClass> ChildList { get; set; }
}

public class ParentClassValidator : AbstractValidator<ParentClass>
{
    public ParentClassValidator()
    {
        RuleFor(x => x.ParentId).NotEmpty().WithMessage("Parent Id cannot be empty");
        When(x => x.ParentId == 1, () =>
          {
              RuleFor(x => x.ActivityId).NotEmpty().WithMessage("Activity To cannot be empty.");
              RuleFor(x => x.ChildList).SetCollectionValidator(new ChildClassValidator().Validate(new ChildClass(), "ParentId1"));
          });
    }
}

public class ChildClass
{
    public int ChildId { get; set; }
    public DateTime DueDate { get; set; }

}

public class ChildClassValidator : AbstractValidator<ChildClass>
{
    public ChildClassValidator()
    {
        RuleFor(x => x.ChildId).NotEmpty().WithMessage("Child Id cannot be empty");
        RuleSet("ParentId1", () =>
        {
            RuleFor(x => x.DueDate).Must(date => date != default(DateTime)).WithMessage("Due date cannot be empty");
        });
    }
}

I am trying to validate the list of child based on the parent model property value. 我正在尝试根据父模型属性值验证子列表。

I need to validate the common rules and also specific ruleset of ALL the childs from the parent. 我需要验证来自父代的所有子代的通用规则和特定规则集。 I tried the below code RuleFor(x => x.ChildList).SetCollectionValidator(new ChildClassValidator().Validate(new ChildClass(), "ParentId1")); 我尝试了以下代码RuleFor(x => x.ChildList).SetCollectionValidator(new ChildClassValidator().Validate(new ChildClass(), "ParentId1")); in parent validator but is not allowed 在父验证器中,但不允许

I went through Child Model Validation using Parent Model Values. 使用父模型值进行了子模型验证。 Fluent Validation. 流利的验证。 MVC4 but it is for only one property. MVC4,但仅用于一个属性。

[Validator(typeof(ParentClassValidator))]
public class ParentClass
{
    public int ParentId { get; set; }
    public int ActivityId { get; set; }
    public DateTime UpateDate { get; set; }
    public IList<ChildClass> ChildList { get; set; }
}

public class ParentClassValidator : AbstractValidator<ParentClass>
{
    public ParentClassValidator()
    {
        RuleFor(x => x.ParentId).NotEmpty().WithMessage("Parent Id cannot be empty");
        When(x => x.ParentId == 1, () =>
        {
              RuleFor(x => x.ActivityId).NotEmpty().WithMessage("Activity To cannot be empty.");
        });
    }

    public override ValidationResult Validate(ValidationContext<ParentClass> context)
    {
        RuleFor(x => x.ChildList).SetCollectionValidator(new ExtMobileTransactionDataValidator(context.InstanceToValidate));
        return base.Validate(context);
    }
}

public class ChildClass
{
    public int ChildId { get; set; }
    public DateTime DueDate { get; set; }

}

public class ChildClassValidator : AbstractValidator<ChildClass>
{
    public ChildClassValidator(ParentClass parent)
    {
        RuleFor(x => x.ChildId).NotEmpty().WithMessage("Child Id cannot be empty");
        if(parent.ParentId == 1)
        {
            RuleFor(x => x.DueDate).Must(date => date != default(DateTime)).WithMessage("Due date cannot be empty");
        }
    }
}

I was able to achieve it by overriding the Validate method. 我可以通过重写Validate方法来实现。

Remember to remove the Validator attribute for child class if the child class will not be validated individually else it will throw "No parameterless constructor defined" error. 如果子类不会被单独验证,请记住删除子类的Validator属性,否则它将抛出“未定义无参数的构造函数”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM