Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Validator missing SetCollectionValidator() method

I'm new to Fluent Validation and just got the version 5.3 from nu Get yesterday. I'm trying to apply an existing validator (PhoneValidator) to a collection property (ICollection) of a class (Employee). The Fluent Validator documentation says to use:

RuleFor(x => x.Orders).SetCollectionValidator(new OrderValidator()); // example usage

However the SetCollectionValidator() method is not available on the version I have. Instead there is only SetValidator() which is marked as [deprecated]. I've seen other posts regarding this same situation and have learned that SetCollectionValidator() is an extension method and need to be sure I have FluentValidation imported. I do.

What am I missing here?

using FluentValidation;
using FluentValidation.Validators;

public class EmployeeValidator : AbstractValidator<Employee>
{
    public EmployeeValidator()
    {            
        // SetCollectionValidator doesn't show in intellisense and won't compile
        RuleFor(e => e.PhoneNumbers).SetCollectionValidator(new PhoneValidator()); 
    }
}

public class PhoneValidator : AbstractValidator<Phone>
{
    public PhoneValidator()
    {
        RuleFor(e => e.Number).Length(10).Matches("^[0-9]$");            
    }
}
like image 960
razaross444 Avatar asked May 29 '14 21:05

razaross444


1 Answers

I'm late to the party but it was the first hit on Google so I thought it'd help others. It seems the API has changed, you now need to do:

RuleForEach(e => e.PhoneNumbers).SetValidator(new PhoneValidator());

A bit nicer in my opinion.

like image 90
eth0 Avatar answered Nov 02 '22 01:11

eth0