Ответ 1
Чтобы решить эту проблему, вы должны изменить класс UserValidation
на общий. См. Код ниже.
public class UserValidation<T> : AbstractValidator<T> where T : UserViewModel
{
public UserValidation()
{
this.RuleFor(x => x.FirstName).NotNull();
this.RuleFor(x => x.FirstName).NotEmpty();
this.RuleFor(x => x.LastName).NotNull();
this.RuleFor(x => x.LastName).NotEmpty();
}
}
[Validator(typeof(UserValidation<UserViewModel>))]
public class UserViewModel
{
public string FirstName;
public string LastName;
}
public class RootViewModelValidation : UserValidation<RootViewModel>
{
public RootViewModelValidation()
{
this.RuleFor(x => x.MiddleName).NotNull();
this.RuleFor(x => x.MiddleName).NotEmpty();
}
}
[Validator(typeof(RootViewModelValidation))]
public class RootViewModel : UserViewModel
{
public string MiddleName;
}