[英]FluentValidation require one out of three values
使用流利的驗證並驗證三個變量中的至少一個是否> 0。我嘗試使用when
otherwise
語句進行驗證,但我無法添加另一個otherwise
語句。
When(c => c.var1!= null || c.var2!= null || c.var3!= null, () =>
{
RuleFor(c => c.var1).GreaterThan(0)
.WithMessage("One of these fields, var1, var2, or var3, are required.");
}).Otherwise(() =>
{
RuleFor(c => c.var2).GreaterThan(0);
});
// I would like to verify a third var but can't stack otherwise statements
如果我想驗證 var1、var2、var3 之一是否 > 0,我應該進行哪些更改?
如果您使用 when 語句,則只需發送一條驗證消息即可驗證所有 3 個變量。 有諸如依賴規則之類的東西,但這更復雜。
RuleFor(c => c.var1).NotNull()
.WithMessage("At least one is required");
RuleFor(c => c.var2).NotNull()
.When(c => c.var2!= null)
.WithMessage("At least one is required");
RuleFor(c => c.var3).NotNull()
.When(c => c.var1!= null && c.var2!= null)
.WithMessage("At least one is required");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.