如何解决具有两个验证器的类型的FluentValidation SetValidator应用了错误的验证器
我有两个验证器来验证同一类型:
public class Foo {
public int Prop1 { get; set; }
public int Prop2 { get; set; }
}
public class FooValidator1 : AbstractValidator<Foo> {
public FooValidator1() {
RuleFor(x => x.Prop1).NotNull();
}
}
public class FooValidator2 : AbstractValidator<Foo> {
public FooValidator2() {
RuleFor(x => x.Prop1).NotNull();
RuleFor(x => x.Prop2).NotNull();
}
}
有一个具有此类属性的父类:
public class Bar {
public Foo Foo { get; set; }
}
我将第一个验证器应用于另一个验证器,如下所示:
public class BarValidator : AbstractValidator<Bar> {
public BarValidator() {
RuleFor(x => x.Foo).SetValidator(new FooValidator1());
}
}
Fluentvalidation配置为在MVC管道中进行验证:
internal static void Cofigure(FluentValidationMvcConfiguration config) {
config.RegisterValidatorsFromAssemblyContaining<FooValidator>();
config.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
config.ValidatorFactoryType = typeof(HttpContextServiceProviderValidatorFactory);
}
Swagger配置为显示fluentvalidation规则:
config.AddFluentValidationRules();
但是出于某种原因,应用了另一条规则的验证:
如果我删除FooValidator2,则将应用正确的验证器。 我在做错什么吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。