liveBook · Manning

AbstractValidator

As explained in ASP.NET Core in Action, 3e

AbstractValidator is the FluentValidation base class that supplies infrastructure and methods for defining validation rules for a model.

AbstractValidator<T> is the FluentValidation base class for defining validation rules in a separate validator class for each model. Its RuleFor() method targets a property, while chained methods such as NotEmpty(), Length(), and Must() describe that property's requirements. section 32.4.1 section 32.5

Currency converter validator

A validator can keep a binding model free of validation attributes while expressing its rules together: CurrencyConverterModelValidator : AbstractValidator<CurrencyConverterModel>. Its constructor can call RuleFor(x => x.CurrencyFrom).NotEmpty().Length(3).Must(...), then apply a custom error message with WithMessage(). section 32.4.1

Listing 32.11 A FluentValidation validator for the currency converter binding model
public class CurrencyConverterModelValidator                       #1
    : AbstractValidator<CurrencyConverterModel>                    #1
{
    private readonly string[] _allowedValues                       #2
        = new []{ "GBP", "USD", "CAD", "EUR" };                    #2
 
    public CurrencyConverterModelValidator()                       #3
    {
        RuleFor(x => x.CurrencyFrom)                               #4
            .NotEmpty()                                            #5
            .Length(3)                                             #5
            .Must(value => _allowedValues.Contains(value))         #6
            .WithMessage("Not a valid currency code");             #6
 
        RuleFor(x => x.CurrencyTo)
            .NotEmpty()
            .Length(3)
            .Must(value => _allowedValues.Contains(value))
            .WithMessage("Not a valid currency code");
 
        RuleFor(x => x.Quantity)
            .NotNull()
            .InclusiveBetween(1, 1000);                            #7
    }
}

Inspect the complete currency converter validator, including allowed currency codes, quantity limits, and chained property rules. section 32.4.1

Extend a validator as requirements become more specific:

  • Use Must() for arbitrary predicates, including rules that compare two model properties. section 32.4.1
  • Extract repeated rules into extension methods such as MustBeCurrencyCode(). section 32.4.1
  • Inject services into the validator when validation depends on application data. section 32.4.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains class structure, chained rules, custom predicates, reuse, dependency injection, and application registration.
Examples
Uses a currency converter model with currency-code, quantity-range, and source-versus-target validation rules.
What it assumes
Assumes a C# model and ASP.NET Core validation setup, while teaching rule declaration separately from model attributes.
What you can do after
Apply it via FluentValidation by creating model validators, chaining RuleFor() requirements, and registering them with dependency injection.
How it connects
Connects model validation with DataAnnotations, dependency injection, extension methods, and ASP.NET Core validation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.