0

I'm really struggling to create interface/convention based rules for FluentValidator. It has following class

   abstract class AbstractValidator<T>
    {

    IRuleBuilderInitial<T, TProperty> RuleFor<TProperty>(Expression<Func<T, TProperty>> expression)
    ...
    }


public interface IWithPropertyA
{
  string PropertyA{get;set;}
}

public interface IWithPropertyB
{
 string PropertyB{get;set;}
}

public class Handler1Data: IWithPropertyA
{
 public string PropertyA {get;set;}
}
public class Handler2Data: IWithPropertyA, IWithPropertyB
{
 public string PropertyA {get;set;}
 public string PropertyB {get;set;}
}

public class Handler1 : AbstractValidator<Handler1Data> {}
public class Handler2 : AbstractValidator<Handler2Data> {}

I'm trying to create extension method which will basically check if generic argument implements specific interface and then adds rule to it:

public static void ValidateAll<T>(this AbstractValidator<T> validator)
        {

           (validator as AbstractValidator<IWithPropertyA>)?.RuleFor(x => x.PropertyA).NotEmpty().WithMessage("PropertyA Missing");
           (validator as AbstractValidator<IWithPropertyB>)?.RuleFor(x => x.PropertyB).NotEmpty().WithMessage("PropertyB Missing");
        }

The problem here is obviously AbstractValidator is not covariant so validator is not castable to neither AbstractValidator<PropertyA> nor AbstractValidator<PropertyB>. I've tried to create my own Base Validator like below and then create extension method based on that but I can't.

public interface IMyValidator<in T>
{
   void AddMyRule<TProperty>(Expression<Func<T, TProperty>> expression) //it doesn't work because Expression<Func<T,Property> cannont be covariant
}

public abstract class MyBaseValidator<T>: AbstractValidator<T> ,IMyValidator<T>
{
   void AddMyRule<TProperty>(Expression<Func<T, TProperty>> expression)
}

Method will be called in each Handler like this:

public class Handler1 : AbstractValidator<Handler1Data> {
  this.ValidateAll();
}
0

2 Answers 2

0

Didn't you mean to write:

(validator as AbstractValidator<IWithPropertyA>)
    ?.RuleFor(x => x.PropertyA)
     .NotEmpty()
     .WithMessage("Property1 Missing");

Because Property1 is not defined anywhere.

0
0

I found one workaround which avoids using Expression which is obviously the issue here. The downfall of it is that we lose property name and we have to configure message manually.

 public interface IMyValidator<out T>
    {
        void AddMyRule<TProperty>(Func<T, TProperty> expression, string message);
    }

    public abstract class MyBaseValidator<T> : AbstractValidator<T>, IMyValidator<T>
        {
            public void AddMyRule<TProperty>(Func<T, TProperty> expression, string message)
            {
                var exp = FuncToExpression(expression);
                RuleFor(exp).NotEmpty().WithMessage(message);
            }

            private static Expression<Func<T, P>> FuncToExpression<T, P>(Func<T, P> f) => x => f(x);
        }

public static class Ext
    {
        public static void ValidateAll<T>(this AbstractValidator<T> validator)
        {
            (validator as IMyValidator<IWithPropertyA>)?.AddMyRule(x => x.PropertyA, "PropA Cant be empty");
            (validator as IMyValidator<IWithPropertyB>)?.AddMyRule(x => x.PropertyB, "PropB Cant be empty");
        }
    }

 public class Handler1 : MyBaseValidator<Handler1Data>
    {
        public Handler1()
        {
            this.ValidateAll();
        }
    }
    public class Handler2 : MyBaseValidator<Handler2Data> { }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.