Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FluentValidation SetCollectionValidator for derived types

How can I set validators on a collection items of derived types?

class BaseClass
{

}

class DerivedClass : BaseClass
{

}

class SomeClass
{
    public IEnumerable<BaseClass> BaseClasses { get; set; }
}

class DerivedClassValidator : AbstractValidator<DerivedClass>
{

}

class SomeClassValidator : AbstractValidator<SomeClass>
{
    public SomeClassValidator()
    {
        RuleFor(x => x.BaseClasses).????.SetCollectionValidator(new DerivedClassValidator);
    }
}

Just wondering...

Is there a way to cast it to a particular type like

RuleFor(x => x.SomeCollection).CastTo(typeof(SomeDerivedType)).SetCollectionValidator(new SomeDerivedValidator());
like image 967
icube Avatar asked Jul 25 '14 04:07

icube


3 Answers

You can use conditional wrapping of rules to validate collection, which contains objects of different derived types.

Suppose you have next class hierarchy:

public class BaseClass
{
    public string Name { get; set; }
}

public class DerivedClassOne : BaseClass
{
    public int Count { get; set; }
}

public class DerivedClassTwo : BaseClass
{
    public double Price { get; set; }
}

And container class with collection of BaseClass objects:

public class ContainerClass
{
    public List<BaseClass> Collection { get; set; } 
}

Main idea is to create one validator class, that responsible for all class hierarchy validation:

public class CommonBaseClassValidator : AbstractValidator<BaseClass>
{
    public CommonBaseClassValidator()
    {
        //common rule for all BaseClass types
        RuleFor(x => x.Name)
            .NotEmpty();

        // special rules for base type
        When(model => model.GetType() == typeof (BaseClass), () =>
        {
            RuleFor(x => x.Name)
                .Length(0, 10);
            // add rules here
        });

        //special rules for derived types
        When(model => model.GetType() == typeof(DerivedClassOne), () =>
        {
            RuleFor(model => ((DerivedClassOne) model).Count)
                .ExclusiveBetween(1, 9);
            // add rules here
        });

        When(model => model.GetType() == typeof(DerivedClassTwo), () =>
        {
            RuleFor(model => ((DerivedClassTwo) model).Price)
                .GreaterThan(1000);
            // add rules here
        });
    }
}

And register this class as a collection item validator:

public class ContainerValidator : AbstractValidator<ContainerClass>
{
    public ContainerValidator()
    {
        RuleFor(model => model.Collection)
            .SetCollectionValidator(new CommonBaseClassValidator());
    }
}
like image 84
Evgeny Levin Avatar answered Nov 16 '22 08:11

Evgeny Levin


I created this to make dealing with this scenario simpler:

public class DerivedValidatorBase<TBase> : AbstractValidator<TBase>
{
    public void MapDerivedValidator<TType, TValidatorType>()
        where TValidatorType : IEnumerable<IValidationRule>, IValidator<TType>, new()
        where TType: TBase
    {
        When(t => t.GetType() == typeof(TType), () => AddDerivedRules<TValidatorType>());
    }

    public void MapDerivedValidator<TType, TValidatorType>(TValidatorType validator)
        where TValidatorType : IEnumerable<IValidationRule>, IValidator<TType>
        where TType: TBase
    {
        When(t => t.GetType() == typeof(TType), () => AddDerivedRules<TValidatorType>(validator));
    }

    private void AddDerivedRules<T>(T validator)
        where T : IEnumerable<IValidationRule>
    {
        foreach (var rule in validator)
        {
            this.AddRule(rule);
        }
    }

    private void AddDerivedRules<T>()
        where T : IEnumerable<IValidationRule>, new()
    {
        IEnumerable<IValidationRule> validator = new T();
        foreach (var rule in validator)
        {
            this.AddRule(rule);
        }
    }
}

Then I simply create a base validator class:

public class CommonBaseClassValidator : DerivedValidatorBase<BaseClass>
{
    public CommonBaseClassValidator()
    {
        MapDerivedValidator<DerivedClass, DerivedClassValidator>();
    }
}

or when using dependency injection:

public class CommonBaseClassValidator : DerivedValidatorBase<BaseClass>
{
    public CommonBaseClassValidator(DerivedClassValidator validator)
    {
        MapDerivedValidator<DerivedClass, DerivedClassValidator>(validator);
    }
}

In use:

RuleFor(v => v.BaseClasses).SetCollectionValidator(new CommonBaseClassValidator());

That way I can re-use existing validators for the derived classes, which I may be using elsewhere, and map them without hassle.

like image 35
DiplomacyNotWar Avatar answered Nov 16 '22 08:11

DiplomacyNotWar


I'd like to add something to help others. I was inspired by Evgeny Levin solution. Instead of a list of rules within When I preferred to separate each validator.

public class CommonBaseClassValidator : AbstractValidator<BaseClass>
{
    public CommonBaseClassValidator()
    {
        //All rules for shared properties
        RuleFor(x => x.Name)
            .NotEmpty();

        RuleFor(x => x.Name)
                .Length(0, 10);

        //special rules for derived types
        When(model => model.GetType() == typeof(DerivedClassOne), 
            () => RuleFor(entity => entity as DerivedClassOne)
                    .SetValidator(new DerivedClassOneValidator()));

        When(model => model.GetType() == typeof(DerivedClassTwo), 
            () => RuleFor(entity => entity as DerivedClassTwo)
                .SetValidator(new DerivedClassTwoValidator()));
    }
}

Hope that helps.

like image 2
Angelo Chiello Avatar answered Nov 16 '22 08:11

Angelo Chiello