Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsMay 23, 2026·3 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
FluentValidation Overview
Comando de instalación directa
npx -y tokrepo@latest install e6f1dfef-56c4-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-run.

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<Program>()
  • 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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados