FluentValidation,需要 3 个中的 2 个

问题描述 投票:0回答:0

我正在尝试制作一个验证器,它可以处理总共 3 个道具中 2 个道具的任意组合。 因此,如果我有 A、B、C,那么如果任何 A-B、B-C、C-A 或 A-B-C 有效,则验证应该成功。

到目前为止我写的是这样的

RuleFor(x=>x.A)
 .Must(val=>!string.IsNullOrEmpty(val)
 .When(x=> !(string.IsNullOrEmpty(x.B) && string.IsNullOrEmpty(x.C)));

RuleFor(x=>x.C)
 .Must(val=>!string.IsNullOrEmpty(val)
 .When(x=> !(string.IsNullOrEmpty(x.B) && string.IsNullOrEmpty(x.A)));

RuleFor(x=>x.B)
 .Must(val=>!string.IsNullOrEmpty(val)
 .When(x=> !(string.IsNullOrEmpty(x.A) && string.IsNullOrEmpty(x.C)));

When(x=> string.IsNullOrEmpty(x.A), ()=>{
   ///   validate A according to business
})
When(x=> string.IsNullOrEmpty(x.B), ()=>{
   ///   validate B according to business
})
When(x=> string.IsNullOrEmpty(x.C), ()=>{
   ///   validate C according to business
})

但这行不通,我找不到让验证器工作的方法

c# fluentvalidation
© www.soinside.com 2019 - 2024. All rights reserved.