Scripts2026年5月23日·1 分钟阅读

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.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
FluentValidation Overview
通用 CLI 安装命令
npx tokrepo install e6f1dfef-56c4-11f1-9bc6-00163e2b0d79

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产