# FluentValidation — Strongly Typed Validation Rules for .NET > A .NET validation library that uses a fluent interface to build strongly typed validation rules for business objects and DTOs. ## Install Save as a script file and run: # FluentValidation — Strongly Typed Validation Rules for .NET ## Quick Use ```bash dotnet add package FluentValidation dotnet add package FluentValidation.DependencyInjectionExtensions ``` ```csharp public class OrderValidator : AbstractValidator { public OrderValidator() { RuleFor(x => x.CustomerName).NotEmpty().MaximumLength(100); RuleFor(x => x.Total).GreaterThan(0); RuleFor(x => x.Items).NotEmpty().WithMessage("Order must have at least one item."); } } var result = new OrderValidator().Validate(order); ``` ## Introduction FluentValidation is a .NET library for building strongly typed validation rules using a fluent API. It separates validation logic from domain models into dedicated validator classes, producing clean and testable validation code. ## What FluentValidation Does - Defines validation rules with a readable fluent syntax - Validates model properties with built-in rules (NotEmpty, MaxLength, GreaterThan, etc.) - Supports conditional validation with When/Unless clauses - Integrates with ASP.NET Core model validation pipeline - Provides custom error messages and localization support ## Architecture Overview FluentValidation uses an AbstractValidator base class where each rule is built from a chain of validators attached to a property expression. At validation time, the validator iterates through all rules and collects failures into a ValidationResult. Rules can be composed, conditionally applied, or grouped into rule sets. The ASP.NET Core integration replaces the default DataAnnotations validator with FluentValidation validators resolved from DI. ## Self-Hosting & Configuration - Install via NuGet: `dotnet add package FluentValidation` - For ASP.NET Core integration: `dotnet add package FluentValidation.AspNetCore` - Register validators in DI: `builder.Services.AddValidatorsFromAssemblyContaining()` - Create one validator class per model or DTO - Compatible with .NET Standard 2.0+ and .NET 6 through .NET 9 ## Key Features - Fluent API that reads like natural language validation rules - Rule sets for validating different scenarios (create vs. update) - Async validation support for rules that query databases or APIs - Custom validators and property validators for complex logic - Built-in severity levels (Error, Warning, Info) per rule ## Comparison with Similar Tools - **Data Annotations** — attribute-based validation built into .NET; simpler but less flexible and harder to test - **Vogen** — value object generator with built-in validation; focuses on domain primitives, not DTOs - **Guard Clauses** — precondition checks that throw exceptions; different purpose, not rule-based - **Humanizer + custom code** — manual validation with friendly messages; no framework, more boilerplate - **MiniValidation** — minimal validation for .NET minimal APIs; lighter, fewer features ## FAQ **Q: Should I use FluentValidation or Data Annotations?** A: FluentValidation is better for complex rules, conditional logic, and unit testing. Data Annotations work well for simple property constraints on small models. **Q: Can I use FluentValidation in a MediatR pipeline?** A: Yes. Create an IPipelineBehavior that resolves the appropriate validator for each request type and runs it before the handler executes. **Q: Does FluentValidation support async rules?** A: Yes. Use MustAsync or CustomAsync to define rules that perform asynchronous operations like database lookups. **Q: Can I localize error messages?** A: Yes. FluentValidation supports resource files, custom message providers, and the WithMessage method accepts localization delegates. ## Sources - https://github.com/FluentValidation/FluentValidation - https://docs.fluentvalidation.net/ --- Source: https://tokrepo.com/en/workflows/asset-e6f1dfef Author: Script Depot