本文源码下载地址:http://www.80cxy.com/Blog/ResourceView?arId=202403191532545995NAAqJh
系列教程地址:http://www.80cxy.com/Blog/ArticleView?arId=202403191517574161ay3s5V
本文简介asp.net中使用FluentValidation验证请求参数的配置。需要再SignUp.Common项目中引用FluentValidation.AspNetCore包,在SignUp.WebApi项目中引用FluentValidation包。
using FluentValidation; namespace SignUp.WebApi.ViewModels { public record PageGridRequest(int PageIndex, int PageSize, string Wheres, string Sort, string Order); public class PageGridRequestValidator : AbstractValidator<PageGridRequest> { public PageGridRequestValidator() { RuleFor(x => x.PageIndex).NotEmpty().WithMessage("PageIndex参数是必须的!"); RuleFor(x => x.PageSize).NotEmpty().WithMessage("PageSize参数是必须的!"); RuleFor(x => x.Sort).NotEmpty().WithMessage("Sort参数是必须的!"); RuleFor(x => x.Order).NotEmpty().WithMessage("Order参数是必须的!"); } } }
//项目接口调用参数验证配置 services.AddMvc().AddFluentValidation(fv => { fv.RegisterValidatorsFromAssemblies(assemblies); }); //请求参数验证方法 services.Configure<ApiBehaviorOptions>(options => { options.InvalidModelStateResponseFactory = (context) => { var errors = context.ModelState .Values .SelectMany(x => x.Errors .Select(p => p.ErrorMessage)) .ToList(); var result = new { Status = true, Code = "412", Message = "参数验证失败!", Data = errors }; return new JsonResult(result); }; });
FluentValidation 有几个内置的验证器,这些验证器的错误消息都可以使用特定占位符。
NotNull 验证程序
说明:确保指定的属性不是 null。
RuleFor(customer => customer.Surname).NotNull();
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{PropertyValue} = 属性的当前值
NotEmpty 验证程序
说明: 确保指定的属性不是 null、空字符串或空格 (或值类型的默认值, 例如 int 0)。
RuleFor(customer => customer.Surname).NotEmpty();
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{PropertyValue} = 属性的当前值
NotEqual 验证程序
说明: 确保指定属性的值不等于特定值 (或不等于其他属性的值)
//Not equal to a particular value
RuleFor(customer => customer.Surname).NotEqual("Foo");
//Not equal to another property
RuleFor(customer => customer.Surname).NotEqual(customer => customer.Forename);
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{ComparisonValue} = 属性不应等于的值
Equal 相等验证程序
说明: 确保指定属性的值等于特定值 (或等于另一个属性的值)
//Equal to a particular value
RuleFor(customer => customer.Surname).Equal("Foo");
//Equal to another property
RuleFor(customer => customer.Password).Equal(customer => customer.PasswordConfirmation);
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{ComparisonValue} = 属性应相等的值
{PropertyValue} = 属性的当前值
Length 长度验证程序
确保特定字符串属性的长度位于指定范围内。但是, 它不能确保字符串属性是否为 null。
RuleFor(customer => customer.Surname).Length(1, 250); //must be between 1 and 250 chars (inclusive)
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{MinLength} = 最小长度
{MaxLength} = 最大长度
{TotalLength} = 输入的字符数
{PropertyValue} = 属性的当前值
MaxLength 最大长度验证程序
说明:确保特定字符串属性的长度不超过指定的值。
RuleFor(customer => customer.Surname).MaximumLength(250); //must be 250 chars or fewer
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{MaxLength} = 最大长度
{TotalLength} = 输入的字符数
{PropertyValue} = 属性的当前值
MinLength 最小长度验证程序
说明:确保特定字符串属性的长度不能小于指定的值。
RuleFor(customer => customer.Surname).MinimumLength(10); //must be 10 chars or more
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{MinLength} = 最小长度
{TotalLength} = 输入的字符数
{PropertyValue} = 属性的当前值
LessThan 小于验证程序
说明: 确保指定属性的值小于特定值 (或小于另一个属性的值)
//Less than a particular value
RuleFor(customer => customer.CreditLimit).LessThan(100);
//Less than another property
RuleFor(customer => customer.CreditLimit).LessThan(customer => customer.MaxCreditLimit);
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{ComparisonValue}-属性比较的值
{PropertyValue} = 属性的当前值
LessThanOrEqualTo 小于等于验证程序
说明: 确保指定属性的值小于等于特定值 (或小于等于另一个属性的值)
//Less than a particular value
RuleFor(customer => customer.CreditLimit).LessThanOrEqualTo(100);
//Less than another property
RuleFor(customer => customer.CreditLimit).LessThanOrEqualTo(customer => customer.MaxCreditLimit);
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{ComparisonValue}-属性比较的值
{PropertyValue} = 属性的当前值
GreaterThan 大于验证程序
说明: 确保指定属性的值大于特定值 (或大于另一个属性的值)
//Greater than a particular value
RuleFor(customer => customer.CreditLimit).GreaterThan(0);
//Greater than another property
RuleFor(customer => customer.CreditLimit).GreaterThan(customer => customer.MinimumCreditLimit);
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{ComparisonValue}-属性比较的值
{PropertyValue} = 属性的当前值
GreaterThanOrEqualTo 大于等于验证程序
说明: 确保指定属性的值大于等于特定值 (或大于等于另一个属性的值)
//Greater than a particular value
RuleFor(customer => customer.CreditLimit).GreaterThanOrEqualTo(1);
//Greater than another property
RuleFor(customer => customer.CreditLimit).GreaterThanOrEqualTo(customer => customer.MinimumCreditLimit);
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{ComparisonValue}-属性比较的值
{PropertyValue} = 属性的当前值
Must 验证程序
描述: 将指定属性的值传递到一个委托中, 可以对该值执行自定义验证逻辑
RuleFor(customer => customer.Surname).Must(surname => surname == "Foo");
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{PropertyValue} = 属性的当前值
请注意, 委托参数不仅传递参数,还支持直接传递验证对象参数:
RuleFor(customer => customer.Surname).Must((customer, surname) => surname != customer.Forename)
正则表达式验证程序
说明: 确保指定属性的值与给定的正则表达式匹配,正则表达式可参阅正则表达式教程这篇文章
RuleFor(customer => customer.Surname).Matches("some regex here");
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{PropertyValue} = 属性的当前值
Email 电子邮件验证程序
说明: 确保指定属性的值是有效的电子邮件地址格式。
RuleFor(customer => customer.Email).EmailAddress();
可用的格式参数占位符:
{PropertyName} = 正在验证的属性的名称
{PropertyValue} = 属性的当前值
自定义验证程序
Must 验证程序
实现自定义验证程序的最简单方法是使用方法 Must 方法,假设我们有以下类:
public class Person {
public IList<Person> Pets {get;set;} = new List<Person>();
}
为了确保列表中至少包含10个元素, 我们可以这样做:
public class PersonValidator:AbstractValidator<Person> {
public PersonValidator() {
RuleFor(x => x.Pets).Must(list => list.Count <= 10).WithMessage("The list must contain fewer than 10 items");
}
}
为了使这种逻辑可重用, 我们可以将其封装为扩展方法。
public static class MyCustomValidators {
public static IRuleBuilderOptions<T, IList<TElement>> ListMustContainFewerThan<T, TElement>(this IRuleBuilder<T, IList<TElement>> ruleBuilder, int num) {
return ruleBuilder.Must(list => list.Count < num).WithMessage("The list contains too many items");
}
}
在这里,我们通过为 IRuleBuilder 创建扩展方法实现可重用逻辑,使用方法很简单。
RuleFor(x => x.Pets).ListMustContainFewerThan(10);
编写自定义验证程序
如果您想灵活控制可重用的验证器, 则可以使用 Must 方法编写自定义规则,此方法允许您手动创建与验证错误关联的实例。
public class PersonValidator:AbstractValidator<Person> {
public PersonValidator() {
RuleFor(x => x.Pets).Custom((list, context) => {
if(list.Count > 10) {
context.AddFailure("The list must contain 10 items or fewer");
}
});
}
}
此方法的优点是它允许您为同一规则返回多个错误。
context.AddFailure("SomeOtherProperty", "The list must contain 10 items or fewer");
// Or you can instantiate the ValidationFailure directly:
context.AddFailure(new ValidationFailure("SomeOtherProperty", "The list must contain 10 items or fewer");
自定义属性验证程序
在某些情况下, 针对某些属性的验证逻辑非常复杂, 我们希望将基于属性的自定义逻辑移动到单独的类中,可通过重写 PropertyValidator 类来完成。
using System.Collections.Generic;
using FluentValidation.Validators;
public class ListCountValidator<T> : PropertyValidator {
private int _max;
public ListCountValidator(int max)
: base("{PropertyName} must contain fewer than {MaxElements} items.") {
_max = max;
}
protected override bool IsValid(PropertyValidatorContext context) {
var list = context.PropertyValue as IList<T>;
if(list != null && list.Count >= _max) {
context.MessageFormatter.AppendArgument("MaxElements", _max);
return false;
}
return true;
}
}
继承 PropertyValidator 时, 必须重写 IsValid 方法,此方法接受一个对象, 并返回一个布尔值, 指示验证是否成功,可通过 PropertyValidatorContext 属性访问:
Instance-正在验证的对象
PropertyDescription-属性的名称 (或者是由调用 WithName 的自定义的别名)
PropertyValue-正在验证的属性值
Member-描述正在验证的属性的 MemberInfo 信息
若要使用自定义的属性验证程序, 可以在定义验证规则时调用:
public class PersonValidator : AbstractValidator<Person> {
public PersonValidator() {
RuleFor(person => person.Pets).SetValidator(new ListCountValidator<Pet>(10));
}
}
本地化与多语言
FluentValidation 为默认验证消息提供几种语言的翻译,默认情况下, 会根据当前线程的 CurrentUICulture 语言文化来选择语言,你也可以使用 WithMessage 和 WithLocalizedMessage 来指定错误提示。
WithMessage
如果使用 Visual Studio 的内置的 resx 格式资源文件, 则可以通过调用 WithMessage 本地化错误消息。
RuleFor(x => x.Surname).NotNull().WithMessage(x => MyLocalizedMessages.SurnameRequired);
当然,您可以将多种语言存储在数据库中,通过 lambda 表达式来读取多语言消息。
WithLocalizedMessage
您可以通过调用 WithLocalizedMessage 方法,传递资源类型和资源名称,使错误消息支持本地多语言。
RuleFor(x => x.Surname).NotNull().WithLocalizedMessage(typeof(MyLocalizedMessages), "SurnameRequired");
默认错误消息
如果您要替换 FluentValidation 的默认错误提示,可以实现 ILanguageManager 接口,该接口支持本地化多语言。
public class CustomLanguageManager : FluentValidation.Resources.LanguageManager {
public CustomLanguageManager() {
AddTranslation("en", "NotNullValidator", "'{PropertyName}' is required.");
}
}
以上代码为 NotNullValidator 验证器自定义英文错误提示消息,当然也可以实现更多的语言,定义好 LanguageManager 后,需要在启动时进行配置。
ValidatorOptions.LanguageManager = new CustomLanguageManager();
禁用本地化多语言
您可以完全禁用 FluentValidation 对本地化的支持, 这将强制使用默认的英文语言, 而不考虑线程的 CurrentUICulture。这可以通过静态类 ValidatorOptions 在应用程序的启动例程中完成。
ValidatorOptions.LanguageManager.Enabled = false;
还可以强制指定默认语言,始终以特定语言显示:
ValidatorOptions.LanguageManager.Culture = new CultureInfo("fr");
附笔者学习 .net core开发时参考相关项目实例源码:asp.net core webapi项目实例源代码锦集下载(72个)
- net core+webapi+nginx windows 服务器部署(1321)
- .Nuget Packages 太占C盘,删除后可以放到其他盘(1194)
- ASP.NET Core 配置 Swagger 显示接口注释描述信息(1094)
- vue调用接口后获取不到后端返回的Header响应头(964)
- .net core 系列实例开发教程-权限管理系统功能介绍(952)
- .net core 6.0 web API + SwaggerUI + IIS部署(912)
- .net core 实例教程(十二)配置启用Swagger中的【Authorize】按钮(831)
- .net core 实例教程(一)新建项目(797)
- .net core 实例教程(十四)配置 Swagger 显示接口注释描述信息及支持版本控制(783)
- .net 6中使用log4net写sqlserver数据库日志(756)
- 2025年3月 (1)
- 2024年6月 (2)
- 2024年5月 (2)
- 2024年4月 (4)
- 2024年3月 (30)
- 2024年1月 (4)
- 2023年12月 (2)
- 2023年11月 (4)
- 2023年10月 (4)
- 2023年9月 (6)
- 2023年3月 (2)
- 2023年2月 (1)
- 2023年1月 (1)
- 2022年12月 (1)
- 2022年9月 (21)
- 2022年8月 (10)
- 2022年7月 (3)
- 2022年4月 (1)
- 2022年3月 (13)
- 2021年8月 (1)
- 2021年3月 (1)
- 2020年12月 (42)
- 2020年11月 (7)
- 2020年10月 (5)
- 2020年8月 (1)
- 2020年6月 (1)
- 2020年3月 (2)
- 2019年12月 (8)
- 2019年11月 (3)
- 2019年9月 (1)
- 2019年4月 (1)
- 2019年3月 (6)
- 2019年2月 (1)
- 2018年7月 (7)
- 1.asp.net mvc内微信pc端、H5、JsApi支付方式总结(5702)
- 2.各大搜索网站网站收录提交入口地址(3201)
- 3.Windows 10休眠文件更改存储位置(3163)
- 4.ECharts仪表盘实例及参数使用详解(3095)
- 5.windows 10安装myeclipse 10破解补丁cracker.jar、run.bat闪退解决办法(2991)
- 6.HTML5 WebSocket与C#建立Socket连接实现代码(2866)
- 7.华为鸿蒙系统清除微信浏览器缓存方法(2779)
- 8.CERT_HAS_EXPIRED错误如何解决(2245)
- 9.Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2188)
- 10.HBuilder编辑器格式化代码(2118)