![](/img/trans.png)
[英]Fluent Validation - Stop validating all other validations when a specific validation fails
[英]Is it possible to stop checking further validations when the first one fails?
否则,我总是需要在执行任何其他验证之前检查值是否为null
。 如果我有许多使用Must()
自定义检查,这有点烦人。
我把NotEmpty()
放在它的最顶层,因此它已经返回false,是否有可能停在那里?
RuleFor(x => x.Name)
.NotEmpty() // Can we not even continue if this fails?
.Length(2, 32)
.Must(x =>
{
var reserved = new[] {"id", "email", "passwordhash", "passwordsalt", "description"};
return !reserved.Contains(x.ToLowerInvariant()); // Exception, x is null
});
看到这里 。 它被称为CascadeMode,可以在这样的单个规则上设置:
RuleFor(x => x.Name)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty()
.Length(2, 32);
或者它可以全局设置:
ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;
注意:如果您全局设置它,则可以在任何单个验证程序类或任何单个规则上使用CascadeMode.Continue
覆盖它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.