流利的验证,对Asp.NET Core中列表中的每个项目进行不同的验证 [英] Fluent Validation, different validation for each item in a list in Asp.NET Core

查看:59
本文介绍了流利的验证,对Asp.NET Core中列表中的每个项目进行不同的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找到一种方法来验证列表中的项目,每个项目具有不同的验证规则.我遇到了Fluent验证,这是一个很棒的库,但是我似乎找不到一种单独对每个项目进行验证的方法.我从类似的线程(使用流利验证来验证2列表)中得到一个模糊的想法,但我不确定如何根据自己的意愿进行调整.

I´ve been trying to find a way to validate items inside a list, each with different validation rules. I came upon Fluent validation which is a great library but I can´t seem to find a way to do validation for each item individually. I got a faint idea from this similar thread (Validate 2 list using fluent validation), but I´m not sure how to focus it how I want.

所以我有这个视图模型:

So I have this View Model:

 public class EditPersonalInfoViewModel
{
    public IList<Property> UserPropertyList { get; set; }
}

这包含Active Directory属性的列表.每个由此类表示:

This contains a list of Active Directory properties. Each represented by this class:

   public class Property
{
    public string Name { get; set; }
    public UserProperties Value { get; set; }
    public string input { get; set; }
    public bool Unmodifiable  { get; set; }
    public string Type { get; set; }
}

重点是,每个AD属性都有不同的约束,因此我想以这种方式为列表中的每个属性指定不同的规则:

The point is that, each AD property has different constraints so I want to specify different rules for each property in the list in some way like this:

   public class ADPropertiesValidator : AbstractValidator<EditPersonalInfoViewModel>
{
    public ADPropertiesValidator()
    {
        RuleFor(p => p.UserPropetyList).Must((p,n) =>
         {
              for (int i = 0; i < n.Count; i++)
                  {
                     if ((n[i].Name.Equals("sAMAccountName"))
                        {
                          RuleFor(n.input ).NotEmpty()....
                        }
                     else if(...)
                        {
                      //More Rules
                        }
                  }
          )

    }
}

任何想法如何解决这个问题?预先感谢.

Any ideas how to approach this? thanks in advance.

推荐答案

您从错误的角度进行验证.无需在收集容器类中创建验证条件,只需创建另一个特定于您的Property类的验证器,然后在ADPropertiesValidator中使用该验证器即可:

You are approaching the validation from the wrong perspective. Instead of creating validation conditions inside your collection container class, just create another validator specific for your Property class, and then use that inside your ADPropertiesValidator:

public class ADPropertyValidator : AbstractValidator<Property>
{
    public ADPropertyValidator()
    {
        When(p => p.Name.Equals("sAMAccountName"), () =>
        {
            RuleFor(p => p.input)
                .NotEmpty()
                .MyOtherValidationRule();
        });

        When(p => p.Name.Equals("anotherName"), () =>
        {
            RuleFor(p => p.input)
                .NotEmpty()
                .HereItIsAnotherValidationRule();
        });
    }
}

public class ADPropertiesValidator : AbstractValidator<EditPersonalInfoViewModel>
{
    public ADPropertiesValidator()
    {
        RuleForEach(vm => vm.UserPropertyList)
            .SetValidator(new ADPropertyValidator());
    }
}

这篇关于流利的验证,对Asp.NET Core中列表中的每个项目进行不同的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆