当另一个 属性 为真时的 Fluentvalidation
Fluent Validator for when another property is true
我正在尝试使用 FluentValidation 验证 属性 'Username' 如果另一个 属性 'Found' 为真。
Object Contains:
string Username
bool Found
RuleFor(x => x.Username)
.NotEmpty().DependentRules(() => {
RuleFor(y => y.Found).Equals(true); //Not valid syntax
})
.WithMessage("Not Found");
不幸的是,似乎没有简单的方法来做到这一点?
使用 When
子句。
RuleFor(x => x.Username).NotEmpty().When(x => x.Found);
依赖规则有点不同;基本上,依赖规则块中指定的规则只有在它们所附加的规则通过时才会被测试。
根据 doco
RuleFor(x => x.Surname).NotNull().DependentRules(() => {
RuleFor(x => x.Forename).NotNull();
});
Here the rule against Forename will only be run if the Surname rule passes.
我正在尝试使用 FluentValidation 验证 属性 'Username' 如果另一个 属性 'Found' 为真。
Object Contains:
string Username
bool Found
RuleFor(x => x.Username)
.NotEmpty().DependentRules(() => {
RuleFor(y => y.Found).Equals(true); //Not valid syntax
})
.WithMessage("Not Found");
不幸的是,似乎没有简单的方法来做到这一点?
使用 When
子句。
RuleFor(x => x.Username).NotEmpty().When(x => x.Found);
依赖规则有点不同;基本上,依赖规则块中指定的规则只有在它们所附加的规则通过时才会被测试。
根据 doco
RuleFor(x => x.Surname).NotNull().DependentRules(() => {
RuleFor(x => x.Forename).NotNull();
});
Here the rule against Forename will only be run if the Surname rule passes.