Stop Fluent Validation on first failure

前端 未结 3 407
暖寄归人
暖寄归人 2021-02-04 02:12

i\'m defining a validation for my Request objects. I would like the validator to stop on the very first failure, not only the one on the same chain. In the example below, if my

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 03:06

    Just add SetValidator before running the rules that depend on them, using a Custom condition:

    //if InpatOrderImportValidatorBllNode fail ,the custom method cannot be executed
    RuleFor(x => x).Cascade(CascadeMode.StopOnFirstFailure)
                   .SetValidator(new InpatOrderImportValidatorBllNode())
                   .Custom((input, context) => {          
                      context.AddFailure(new ValidationFailure("DrugTypeName", "fail"));
                   });
    

提交回复
热议问题