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

This is a prerelease version of Tenon.FluentValidation.AspNetCore.Extensions.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Tenon.FluentValidation.AspNetCore.Extensions --version 0.0.1-alpha-202502101434                
NuGet\Install-Package Tenon.FluentValidation.AspNetCore.Extensions -Version 0.0.1-alpha-202502101434                
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-202502101434" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tenon.FluentValidation.AspNetCore.Extensions --version 0.0.1-alpha-202502101434                
#r "nuget: Tenon.FluentValidation.AspNetCore.Extensions, 0.0.1-alpha-202502101434"                
#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.
// Install Tenon.FluentValidation.AspNetCore.Extensions as a Cake Addin
#addin nuget:?package=Tenon.FluentValidation.AspNetCore.Extensions&version=0.0.1-alpha-202502101434&prerelease

// Install Tenon.FluentValidation.AspNetCore.Extensions as a Cake Tool
#tool nuget:?package=Tenon.FluentValidation.AspNetCore.Extensions&version=0.0.1-alpha-202502101434&prerelease                

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. 
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-202502101554 0 2/10/2025
0.0.1-alpha-202502101448 0 2/10/2025
0.0.1-alpha-202502101434 0 2/10/2025
0.0.1-alpha-202501130258 41 1/13/2025
0.0.1-alpha-202412311524 71 12/31/2024
0.0.1-alpha-202412061617 56 12/6/2024
0.0.1-alpha-202412051527 57 12/5/2024
0.0.1-alpha-202412051432 49 12/5/2024
0.0.1-alpha-202412041445 54 12/4/2024
0.0.1-alpha-202412021409 54 12/2/2024
0.0.1-alpha-202411301019 57 11/30/2024
0.0.1-alpha-202411170525 52 11/17/2024
0.0.1-alpha-202411161308 50 11/16/2024
0.0.1-alpha-202411131604 55 11/13/2024
0.0.1-alpha-202411111439 66 11/11/2024
0.0.1-alpha-202411051434 61 11/5/2024
0.0.1-alpha-202410281339 57 10/28/2024
0.0.1-alpha-202410131500 58 10/13/2024
0.0.1-alpha-202407261457 62 7/26/2024
0.0.1-alpha-202407261325 58 7/26/2024
0.0.1-alpha-202406271301 63 6/27/2024
0.0.1-alpha-202406251508 61 6/25/2024
0.0.1-alpha-202406251310 59 6/25/2024
0.0.1-alpha-202406141611 59 6/14/2024
0.0.1-alpha-202406141550 60 6/14/2024
0.0.1-alpha-202406121515 61 6/12/2024
0.0.1-alpha-202406061553 66 6/6/2024
0.0.1-alpha-202406041519 63 6/4/2024
0.0.1-alpha-202406011613 66 6/1/2024
0.0.1-alpha-202406011238 61 6/1/2024
0.0.1-alpha-202405311458 62 5/31/2024
0.0.1-alpha-202405291213 74 5/29/2024
0.0.1-alpha-202405190458 65 5/19/2024
0.0.1-alpha-202405161229 59 5/16/2024
0.0.1-alpha-202405141510 57 5/14/2024
0.0.1-alpha-202405101323 66 5/10/2024
0.0.1-alpha-202405081356 70 5/8/2024
0.0.1-alpha-202405021337 32 5/2/2024
0.0.1-alpha-202405021336 29 5/2/2024
0.0.1-alpha-202405020452 45 5/2/2024
0.0.1-alpha-202405011443 50 5/1/2024
0.0.1-alpha-202404291541 62 4/29/2024
0.0.1-alpha-202404281218 60 4/28/2024