
Security News
Opengrep Adds Apex Support and New Rule Controls in Latest Updates
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
tenon.fluentvalidation.aspnetcore.extensions
Advanced tools
ASP.NET Core 的 FluentValidation 扩展库,提供了便捷的验证和本地化支持。
dotnet add package Tenon.FluentValidation.AspNetCore.Extensions
Program.cs
中注册服务:// 添加本地化支持
builder.Services.AddLocalization();
// 添加 FluentValidation
builder.Services.AddWebApiFluentValidation(
builder.Configuration.GetSection("FluentValidation"),
typeof(Program).Assembly);
appsettings.json
中配置:{
"FluentValidation": {
"DisableDefaultModelValidation": true,
"ValidatorLifetime": "Scoped"
}
}
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"]);
}
}
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]).*$") |
支持通过资源文件进行验证消息的本地化,可以通过以下方式指定语言:
?culture=zh-CN
c=zh-CN|uic=zh-CN
Accept-Language: zh-CN
可以在 samples/FluentValidationSample 中查看完整的示例项目。
FAQs
Unknown package
We found that tenon.fluentvalidation.aspnetcore.extensions demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.