Allow Abstract Validators where generic is an Interface to run as part of request cycle.
When creating a validator class that extends AbstractValidator<T>, the generic must be a concrete class for the validation to actually run once that validator is registered. It would be nice if I could define several interfaces for my messages that I want specific shared validation run against, and then just register a validator for that interface, and have the validation run against any message that comes in that implements that interface.
I know there are ways to do this through request filters, as well as through attributes on the messages, but the attributes requires having to pull more dependencies into the project (or defining all of the filters in that project) with all of the messages and dtos. We are using a nuget package for consumers just of this project, so we are trying to keep it as light as possible.
Interface
public interface IHaveSharedProperty
Message Class
public class MessageWithSharedPropery : IHaveSharedProperty, IReturn<MessageResponse>
Validator
public class SharedPropertyValidator : AbstractValidator<IHaveSharedProperty>

-
wwwlicious commented
The way this is recommended to be handled in FluentValidator is to create a custom base class
public abstract class AbstactSharedPropertyValidator : AbstractValidator<IHaveSharedProperty>
Then implement this for each DTO where applicable.This avoids potential ordering issues with cascading rules and knowing which should be applied first. It is for this reason that validation is usually explicit to the concrete implementation.
public class MessageWithSharedProperyValidator : AbstactSharedPropertyValidator <MessageWithSharedProperty>
Whilst you still have to create validators for each concrete class, you can avoid duplicating validation logic in them by putting it in the base class.
Hope that helps.