【问题标题】:FluentValidation - how to have one entity property value drive another entities validationFluentValidation - 如何让一个实体属性值驱动另一个实体验证
【发布时间】:2017-02-10 22:51:12
【问题描述】:

我有一个由多个属性和多个集合组成的对象的简化示例:

"Form": {
  "Funders": [
    {
      "FunderID": "string",
      "Name": "string",
      "Type": "string"
    }
  ],
  "Publications": [
    {
      "PublicationID": "string",
      "Name": "string",
      "Type": "string",
    }
  ],
  "Created": "2017-02-10T22:26:49.528Z",
  "CreatedBy": "string",
  "LastModified": "2017-02-10T22:26:49.528Z",
  "LastModifiedBy": "string",
}

如果Funders 中的任何Funder 使用流利的验证具有Type 的A,我如何验证Publications 至少有1 个PublicationType Z。

如果任何资助者类型为 A 类型,则以不同方式说明,那么出版物中的出版物之一必须为 Z 类型

目前我有出版物、资助者和表单的验证器,只是在努力弄清楚如何创建此验证。

我正在使用 fluentvalidation、c#、实体框架。

【问题讨论】:

    标签: c# entity-framework fluentvalidation


    【解决方案1】:

    答案是从您对整个模型的验证中执行验证。其实很简单。

       RuleFor(x => x.Publications
       .Where(p => p.Type == "Type Z").Count()).GreaterThan(0)
       .When(x => x.Funders.Where(f => f.Type == "Type A").Count() > 0)
       .WithMessage("An Form containing a funder of type 'Type A' must have at least one Publication with of type 'Type Z'.")
       .OverridePropertyName("Publications");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      相关资源
      最近更新 更多