使用流畅验证并验证是否至少三个变量中有一个是>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中的一个是>我应该做什么改变?
如果你使用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");