【发布时间】:2023-02-21 18:08:28
【问题描述】:
我有一组继承相同基类的类。每个具体类都有自己的验证类。另一个类有一个基类的集合,我想用具体的验证器验证每个元素。
爬网时,我在 FluentValidation GitHub 问题跟踪器上找到了 question。解决方案是使用子验证器适配器.不幸的是,他们 changed 类,因此它使用通用方法。也有了这个改变适当的价值在 IValidationContext 上不再可用,这将允许检索每个条目的当前类型。
考虑用 *.SetValidator(new ...).When(...) 或 ChildRules 来解决它。但是根据我的理解,这两种方式在这种情况下都无济于事。
任何提示需要什么才能使用版本 11 实现这种验证行为?
public abstract class Base { ... }
public class A : Base {}
public class AValidator : AbstractValidator<a> { ... }
public class B : Base {}
public class BValidator : AbstractValidator<a> { ... }
public class SomeOther
{
List<Base> Elements { get; set; } = new List<Base>();
}
public class SomeOtherValidator : AbstractValidator<SomeOther>
{
public SomeOtherValidator()
{
// use AValidator or BValidator dependend on the concrete type of each element
RuleForEach(x => x.Elements).SetValidator(??);
}
}
【问题讨论】:
标签: c# fluentvalidation