FluentValidation v3: RuleSets

Now that FluentValidation 2.0 is available I’ve started working on FluentValidation v3. At the moment, there are no binary releases for v3 but the source code is available on the v3 branch on github.

FluentValidation 3.0 will be adding several major new features, but will only be available on .NET 4 (FluentValidation 2.0 is the last version that will run on .NET 3.5).  This is the first in a series of posts will cover some of the new upcoming features.


FluentValidation v3 supports the concept of RuleSets which allow you to group validation rules together which can be executed together as a group whilst ignoring other rules.

For example, let’s imagine we have 3 properties on a Person object (Id, Surname and Forename) and have a validation rule for each. We could group the Surname and Forename rules together in a “Names” RuleSet:

public class PersonValidator : AbstractValidator<Person> {
  public PersonValidator() {
     RuleSet("Names", () => {
        RuleFor(x => x.Surname).NotNull();
        RuleFor(x => x.Forename).NotNull();
     RuleFor(x => x.Id).NotEqual(0);

Here the two rules on Surname and Forename are grouped together in a “Names” RuleSet. We can invoke only these rules by passing a ruleSet parameter to the Validate extension method (note that this must be a named parameter as this overload has several options available).

var validator = new PersonValidator();
var person = new Person();
var result = validator.Validate(person, ruleSet: "Names");

This allows you to break down a complex validator definition into smaller segments that can be executed in isolation.

Next time we’ll take a look at the new CustomizeValidatorAttribute and how this can be used to gain greater control of FluentValidation 3.0’s ASP.NET MVC integration.

Written on February 3, 2011