One approach to take would be as follows:
public class Base
{
public string BaseName { get; set; }
}
public class Derived1 : Base
{
public string Derived1Name { get; set; }
}
public class BaseValidator<T> : AbstractValidator<T> where T : Base
{
public BaseValidator()
{
RuleFor(b => b.BaseName).NotNull();
}
}
public class Derived1Validator : BaseValidator<Derived1>
{
public Derived1Validator()
{
RuleFor(d => d.Derived1Name).NotNull();
}
}
So you first create your base validator, make it accept a generic type argument and specify that the generic type must be of type base
. Set up your general rules for your base class and move on.
For any validators that validate children of your base class, you have those validators inherit from the baseValidator, where T will be your derived class type.