I'm using Fluent validation library to validate incoming requests from webApi with the following model structure to validate:
public class BaseClass
{
public MyEnum MyProperty { get; set; }
}
public class DerivedClass : BaseClass
{
public new string MyProperty { get; set; }
}
Please note the new keyword for DerivedClass.MyProperty property.
Here is my fluent validation implementation:
public class BaseClassValidator<T> : AbstractValidator<T> where T : BaseClass
{
public BaseClassValidator()
{
RuleFor(prop => prop.MyProperty).IsInEnum();
}
}
public class DerivedClassValidator<T> : BaseClassValidator<DerivedClass>
{
public DerivedClassValidator()
{
RuleFor(prop => prop.MyProperty).NotEmpty();
}
}
Incomming model to be validate is of type DerivedClass.
I have several other properties though, i cut them off for more clarification.
The problem is, the FluentValidator calls the validations for both BaseClass and DerivedClass, while desired behavior is to validate only DerivedClass's properties when a property is overridden with new keyword and leave the BaseClass's property with the same name unchecked.
The question is: Is there any built-in functionality support this scenario "to suppress validation of BaseClass's properties which are overridden in the DerivedClass, instead use corresponding DerivedClass's validation for those properties"?