Tenon.FluentValidation.AspNetCore.Extensions 0.0.1-alpha-202502241449

This is a prerelease version of Tenon.FluentValidation.AspNetCore.Extensions.
dotnet add package Tenon.FluentValidation.AspNetCore.Extensions --version 0.0.1-alpha-202502241449
                    
NuGet\Install-Package Tenon.FluentValidation.AspNetCore.Extensions -Version 0.0.1-alpha-202502241449
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tenon.FluentValidation.AspNetCore.Extensions" Version="0.0.1-alpha-202502241449" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tenon.FluentValidation.AspNetCore.Extensions" Version="0.0.1-alpha-202502241449" />
                    
Directory.Packages.props
<PackageReference Include="Tenon.FluentValidation.AspNetCore.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tenon.FluentValidation.AspNetCore.Extensions --version 0.0.1-alpha-202502241449
                    
#r "nuget: Tenon.FluentValidation.AspNetCore.Extensions, 0.0.1-alpha-202502241449"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Tenon.FluentValidation.AspNetCore.Extensions@0.0.1-alpha-202502241449
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tenon.FluentValidation.AspNetCore.Extensions&version=0.0.1-alpha-202502241449&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Tenon.FluentValidation.AspNetCore.Extensions&version=0.0.1-alpha-202502241449&prerelease
                    
Install as a Cake Tool

Tenon.FluentValidation.AspNetCore.Extensions

ASP.NET Core 的 FluentValidation 扩展库,提供了便捷的验证和本地化支持。

功能特性

  • 自动注册验证器
  • 支持验证消息本地化
  • 自定义验证响应格式
  • 灵活的配置选项
  • 内置常用验证扩展方法

安装

dotnet add package Tenon.FluentValidation.AspNetCore.Extensions

使用方法

  1. Program.cs 中注册服务:
// 添加本地化支持
builder.Services.AddLocalization();

// 添加 FluentValidation
builder.Services.AddWebApiFluentValidation(
    builder.Configuration.GetSection("FluentValidation"),
    typeof(Program).Assembly);
  1. appsettings.json 中配置:
{
  "FluentValidation": {
    "DisableDefaultModelValidation": true,
    "ValidatorLifetime": "Scoped"
  }
}
  1. 创建验证器:
public class UserRegistrationValidator : AbstractValidator<UserRegistrationRequest>
{
    public UserRegistrationValidator(IStringLocalizer<ValidationMessages> localizer)
    {
        RuleFor(x => x.Username)
            .Required()
            .WithMessage(localizer["Username_Required"])
            .Length(3, 20)
            .WithMessage(localizer["Username_Length", 3, 20]);

        RuleFor(x => x.Email)
            .Required()
            .WithMessage(localizer["Email_Required"])
            .EmailAddress()
            .WithMessage(localizer["Email_Invalid"]);

        RuleFor(x => x.Password)
            .Required()
            .WithMessage(localizer["Password_Required"])
            .MinimumLength(6)
            .WithMessage(localizer["Password_Length", 6])
            .Matches(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{6,}$")
            .WithMessage(localizer["Password_Complexity"]);
    }
}
  1. 添加本地化资源文件:
Resources/
  ├── ValidationMessages.cs          # 资源标记类
  └── ValidationMessages.zh-CN.resx  # 中文资源文件

资源文件示例(ValidationMessages.zh-CN.resx):

<?xml version="1.0" encoding="utf-8"?>
<root>
  <data name="Username_Required" xml:space="preserve">
    <value>请输入用户名</value>
  </data>
  <data name="Username_Length" xml:space="preserve">
    <value>用户名长度必须在 {0} 到 {1} 个字符之间</value>
  </data>
  <data name="Email_Required" xml:space="preserve">
    <value>请输入电子邮件地址</value>
  </data>
  <data name="Email_Invalid" xml:space="preserve">
    <value>请输入有效的电子邮件地址</value>
  </data>
</root>

配置选项

选项 说明 默认值
DisableDefaultModelValidation 是否禁用 ASP.NET Core 默认的模型验证响应 true
ValidatorLifetime 验证器生命周期 Scoped

验证器扩展方法

方法 说明 示例
Required 必填验证 .Required()
Length 长度验证 .Length(3, 20)
MinimumLength 最小长度验证 .MinimumLength(6)
Matches 正则表达式验证 .Matches(@"^(?=.*[a-z])(?=.*[A-Z]).*$")

本地化支持

支持通过资源文件进行验证消息的本地化,可以通过以下方式指定语言:

  1. 查询字符串:?culture=zh-CN
  2. Cookie:c=zh-CN|uic=zh-CN
  3. Accept-Language 头:Accept-Language: zh-CN

示例项目

可以在 samples/FluentValidationSample 中查看完整的示例项目。

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1-alpha-202502241449 77 2/24/2025
0.0.1-alpha-202502101554 80 2/10/2025
0.0.1-alpha-202502101448 74 2/10/2025
0.0.1-alpha-202502101434 81 2/10/2025
0.0.1-alpha-202501130258 69 1/13/2025
0.0.1-alpha-202412311524 92 12/31/2024
0.0.1-alpha-202412061617 74 12/6/2024
0.0.1-alpha-202412051527 76 12/5/2024
0.0.1-alpha-202412051432 64 12/5/2024
0.0.1-alpha-202412041445 72 12/4/2024
0.0.1-alpha-202412021409 74 12/2/2024
0.0.1-alpha-202411301019 73 11/30/2024
0.0.1-alpha-202411170525 70 11/17/2024
0.0.1-alpha-202411161308 64 11/16/2024
0.0.1-alpha-202411131604 68 11/13/2024
0.0.1-alpha-202411111439 82 11/11/2024
0.0.1-alpha-202411051434 71 11/5/2024
0.0.1-alpha-202410281339 70 10/28/2024
0.0.1-alpha-202410131500 70 10/13/2024
0.0.1-alpha-202407261457 74 7/26/2024
0.0.1-alpha-202407261325 68 7/26/2024
0.0.1-alpha-202406271301 76 6/27/2024
0.0.1-alpha-202406251508 72 6/25/2024
0.0.1-alpha-202406251310 71 6/25/2024
0.0.1-alpha-202406141611 67 6/14/2024
0.0.1-alpha-202406141550 74 6/14/2024
0.0.1-alpha-202406121515 71 6/12/2024
0.0.1-alpha-202406061553 74 6/6/2024
0.0.1-alpha-202406041519 73 6/4/2024
0.0.1-alpha-202406011613 77 6/1/2024
0.0.1-alpha-202406011238 73 6/1/2024
0.0.1-alpha-202405311458 73 5/31/2024
0.0.1-alpha-202405291213 85 5/29/2024
0.0.1-alpha-202405190458 74 5/19/2024
0.0.1-alpha-202405161229 68 5/16/2024
0.0.1-alpha-202405141510 72 5/14/2024
0.0.1-alpha-202405101323 75 5/10/2024
0.0.1-alpha-202405081356 86 5/8/2024
0.0.1-alpha-202405021337 46 5/2/2024
0.0.1-alpha-202405021336 42 5/2/2024
0.0.1-alpha-202405020452 54 5/2/2024
0.0.1-alpha-202405011443 63 5/1/2024
0.0.1-alpha-202404291541 73 4/29/2024
0.0.1-alpha-202404281218 71 4/28/2024