我有一个关于FluentValidation的问题。我目前有以下验证:
RuleFor(x => x.value)
.NotNull()
.WithMessage("This field is mandatory.");我如何添加一个条件,以便只在x.mandatory属性的值等于true时才执行验证?
发布于 2020-08-12 03:22:59
FluentValidation支持使用When和Unless方法的条件。有关详细信息https://docs.fluentvalidation.net/en/latest/conditions.html,请参阅文档中的此页面
您可以通过在NotNull之后链接对When的调用来实现您想要的结果:
RuleFor(x => x.value)
.NotNull()
.When(x => x.mandatory)
.WithMessage("This field is mandatory.");https://stackoverflow.com/questions/63364988
复制相似问题