FluentValidation 集合 collections

简单类型的集合

我们可以用RuleForEach 方法来给集合里面每个元素添加验证规则。

public class Person 
{
  public List<string> AddressLines { get; set; } = new List<string>();
}

public class PersonValidator : AbstractValidator<Person> 
{
  public PersonValidator()
  {
    RuleForEach(x => x.AddressLines).NotNull();
  }
}

上面的规则将对集合里面的每个元素进行NotNull的检验。 从8.5版开始,如果要知道Collections中的哪一个元素出问题,则可以使用特殊的占位符:


public class PersonValidator : AbstractValidator<Person> 
{
  public PersonValidator() 
  {
    RuleForEach(x => x.AddressLines).NotNull().WithMessage("Address {CollectionIndex} is required.");
  }
}

复杂类型的集合

我们可以同时使用 RuleForEachSetValidator 来验证复杂类型的集合 例如:

public class Customer 
{
  public List<Order> Orders { get; set; } = new List<Order>();
}

public class Order
{
  public double Total { get; set; }
}

public class OrderValidator : AbstractValidator<Order>
{
  public OrderValidator()
  {
    RuleFor(x => x.Total).GreaterThan(0);
  }
}

public class CustomerValidator : AbstractValidator<Customer> 
{
  public CustomerValidator() 
  {
    RuleForEach(x => x.Orders).SetValidator(new OrderValidator());
  }
}

另外在 FluentValidation 8.5 以后,我们同样可以用 ChildRules 来为集合的每个元素进行验证

public class CustomerValidator : AbstractValidator<Customer>
{
  public CustomerValidator() 
  {
    RuleForEach(x => x.Orders).ChildRules(orders =>
    {
      orders.RuleFor(x => x.Total).GreaterThan(0);
    });
  }
}

我们可以使用where条件来对某些Collections进行验证 需要注意的是,这个where必须马上跟在RuleForEach后面

RuleForEach(x => x.Orders)
  .Where(x => x.Cost != null)
  .SetValidator(new OrderValidator());

在版本8.2后,我们可以直接在RuleFor后用ForEach 来代替 RuleForEach

// 这条规则对整个collection启作用
RuleFor(x => x.Orders)
  .Must(x => x.Count <= 10).WithMessage("No more than 10 orders are allowed");

// 这条规则对每个元素取作用 (RuleForEach)
RuleForEach(x => x.Orders)
  .Must(order => order.Total > 0).WithMessage("Orders must have a total of more than 0")

上面的可以改写成

RuleFor(x => x.Orders)
  .Must(x => x.Count <= 10).WithMessage("No more than 10 orders are allowed")
  .ForEach(orderRule => {
    orderRule.Must(order => order.Total > 0).WithMessage("Orders must have a total of more than 0")
  });
最近更新的
...