FluentValidation 3.1 available

FluentValidation 3.1 is now available for downloaded either from the CodePlex page or through NuGet.

The biggest new feature of 3.1 is being able to share a condition across multiple rules. In previous versions, if you wanted to re-use a condition across multiple rules then you’d have to declare the condition multiple times:

public class PersonValidator : AbstractValidator<Person> {
    public PersonValidator() {
        RuleFor(x => x.Surname).NotNull().When(x => x.Id > 0);
        RuleFor(x => x.Forename).NotNull().When(x => x.Id > 0);
    }
}

…but with 3.1 you can declare the condition once using a nested closure:

public class PersonValidator : AbstractValidator<Person> {
    public PersonValidator() {
        When(x => x.Id > 0, () => {
           RuleFor(x => x.Surname).NotNull();
           RuleFor(x => x.Forename).NotNull();
        });
    }
}

In addition, FluentValidation’s support for nullable types (which was enhancd with 3.0) has been extended to work with cross-property rules.

Written on July 30, 2011