【问题标题】:Fluent validation add rule with index流利的验证添加规则与索引
【发布时间】:2019-06-11 07:56:38
【问题描述】:

我为这个模型做了一个流利的验证类

 public class OnlineCashierRequest
 {
    Guid InvoiceId { get; set; }      
    string ClientPhoneOrEmail { get; set; }    
    List<IOnlineCashierProduct> Products { get; set; }
 }

public interface IOnlineCashierProduct
{
     string Name { get; set; }
     decimal Amount { get; set; }
     int Count { get; set; }
}

我有一个 products 的规则,其中 Amount 等于 null。但是我怎样才能在消息行索引中写$"Amount is less zero for row {rowIndex}"

RuleForEach(t => t.Products)
    .Must(x => x.Amount < 0)
    .WithMessage(x => $"[{rowIndex}] Amount must be greater then zero\r\n");

【问题讨论】:

  • 为什么需要行索引?
  • @chris 日志验证异常
  • 在日志消息中使用IOnlineCashierProductName不是更好吗?
  • @chris 不,也可能是产品名称为空的错误

标签: c# fluentvalidation


【解决方案1】:

https://docs.fluentvalidation.net/en/latest/collections.html

RuleForEach(t => t.Products)
    .Must(x => x.Amount < 0)
    .WithMessage(x => $"[{CollectionIndex}] Amount must be greater than zero\r\n");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多