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 186 2/24/2025
0.0.1-alpha-202502101554 143 2/10/2025
0.0.1-alpha-202502101448 139 2/10/2025
0.0.1-alpha-202502101434 139 2/10/2025
0.0.1-alpha-202501130258 133 1/13/2025
0.0.1-alpha-202412311524 147 12/31/2024
0.0.1-alpha-202412061617 140 12/6/2024
0.0.1-alpha-202412051527 140 12/5/2024
0.0.1-alpha-202412051432 120 12/5/2024
0.0.1-alpha-202412041445 136 12/4/2024
0.0.1-alpha-202412021409 137 12/2/2024
0.0.1-alpha-202411301019 141 11/30/2024
0.0.1-alpha-202411170525 132 11/17/2024
0.0.1-alpha-202411161308 123 11/16/2024
0.0.1-alpha-202411131604 127 11/13/2024
0.0.1-alpha-202411111439 141 11/11/2024
0.0.1-alpha-202411051434 132 11/5/2024
0.0.1-alpha-202410281339 128 10/28/2024
0.0.1-alpha-202410131500 137 10/13/2024
0.0.1-alpha-202407261457 144 7/26/2024
Loading failed