liveBook · Manning

Must

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

Must is a FluentValidation rule method that applies a predicate to implement custom validation logic.

Must is a FluentValidation rule method that applies a predicate to custom validation logic. Chain it after RuleFor() and other requirements such as NotEmpty() or Length(). Validation succeeds when the predicate returns true. section 32.4.1 section 32.5

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
    }
}

The currency validator combines basic checks with Must() to accept only GBP, USD, CAD, or EUR and attach a custom error message. section 32.4.1

Use Must() when validation depends on custom application rules:

  • Validate a property against an allowed collection, such as supported currency codes. section 32.4.1
  • Compare multiple values by accepting the containing model and the current property value, such as requiring CurrencyTo to differ from CurrencyFrom. section 32.4.1
  • Extract repeated predicates into extension methods when several validators share the same rule. section 32.4.1
  • Use injected services when a rule needs application data, such as currencies supplied by ICurrencyProvider. section 32.4.1

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

How deep it goes
Explains property and cross-property predicates, reusable extensions, and service-backed rules without detailing library internals.
Examples
Uses currency-code membership, injected currency providers, and a CurrencyTo-versus-CurrencyFrom comparison.
What it assumes
Assumes basic C# lambdas and FluentValidation RuleFor chains while introducing custom predicates from the validation-rule level.
What you can do after
Apply it via FluentValidation to build custom property and cross-property validation rules.
How it connects
Connects Must() with FluentValidation, RuleFor, DataAnnotations, extension methods, and dependency injection.

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.