liveBook · Manning

RuleFor

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

RuleFor is a FluentValidation method that begins defining validation rules for a model property or member.

RuleFor begins a FluentValidation rule for a model property. Call it inside a validator derived from AbstractValidator<T>, select the property with a lambda, and chain requirements such as NotEmpty(), Length(), and Must(). section 32.4.1

Currency conversion rule

RuleFor(x => x.CurrencyTo) .NotEmpty() .Length(3) .Must(value => allowedValues.Contains(value)) .WithMessage("Not a valid currency code"); The chain combines several requirements for one property. Must() can also compare the selected value with the complete model, such as preventing CurrencyTo from matching CurrencyFrom. section 32.4.1

Use an extension method when a RuleFor chain contains a rule reused across validators. FluentValidation keeps these rules separate from model attributes and restricts each chain to rules that make sense for its selected property. section 32.4.1

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

How deep it goes
Explains property selection, chained requirements, custom predicates, cross-property checks, and reusable extensions.
Examples
Uses currency-code, quantity-range, reusable currency-rule, dependency-injected provider, and different-property validation examples.
What it assumes
Assumes basic C# lambdas and model properties while introducing validator classes and fluent chaining.
What you can do after
Build property validation chains and custom predicates in FluentValidation validators.
How it connects
Connects RuleFor with FluentValidation, AbstractValidator, Must, extension methods, dependency injection, and DataAnnotations.

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.