【问题标题】:Validating collections with FluentValidation使用 FluentValidation 验证集合
【发布时间】:2018-11-10 00:14:25
【问题描述】:

我正在尝试做这样的事情:

RuleFor(client => client.PriceCustomization.GroupBy(a=>a.ProductId).Select(a => a.ToList()))
   .SetCollectionValidator(new PriceCustomizationsForProductValidator());

但我收到此错误:失败:System.InvalidOperationException:嵌套验证器只能与成员表达式一起使用。

“PriceCustomizationsForProductValidator”是“PriceCustomization”列表的验证器

有谁知道我该如何解决这个问题?

【问题讨论】:

    标签: c# fluentvalidation


    【解决方案1】:

    使用FluentValidation 版本 8,试试这个:

    RuleForEach(client => client.PriceCustomization)
       .SetValidator(new PriceCustomizationsForProductValidator());
    

    见https://fluentvalidation.net/upgrading-to-8.html

    【讨论】:

      【解决方案2】:

      如果PriceCustomizationsForProductValidator 是列表类型的验证器,则无需调用.SetCollectionValidator。相反,您应该可以拨打.SetValidator。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多