FluentValidation rule for null object

后端 未结 9 1144
遥遥无期
遥遥无期 2020-12-25 10:44

I\'ve been trying to work out how to create a FluentValidation rule that checks if the instance of an object it\'s validating is not null, prior to validating it\'s properti

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-25 11:19

    Use the Cascade mode.

    Here is the example from the documentation.

    RuleFor(x => x.Surname).Cascade(CascadeMode.StopOnFirstFailure).NotNull().NotEqual("foo");
    

    Also from the documentation:

    If the NotNull validator fails then the NotEqual validator will not be executed. This is particularly useful if you have a complex chain where each validator depends on the previous validator to succeed.

提交回复
热议问题