簡體   English   中英

FluentValidation-如何使一個實體屬性值驅動另一實體驗證

[英]FluentValidation - how to have one entity property value drive another entities validation

我有一個包含多個屬性和多個集合的對象,一個簡化的示例:

"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中的任何Funders使用流利的驗證而具有A Type ,那么我如何驗證Publications至少具有1種Type Z的Publication

如果任何資助者類型為A類型,則以另一種方式陳述,則出版物中的一個出版物必須為Z類型。

目前,我有一些針對Publisher,Funder和Form的驗證器,只是想弄清楚如何創建此驗證。

我正在使用fluentvalidation,C#,實體框架。

答案是從整個模型的驗證中執行驗證。 實際上很簡單。

   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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM