.net core 实例教程(七)接口调用参数验证配置,FluentValidation配置及使用

本文源码下载地址: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包。

一、在SignUp.WebApi创建请求参数类,并配置验证规则。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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参数是必须的!");

        }

    }

}

二、注册FluentValidation验证

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

//项目接口调用参数验证配置

            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个)

  • 26
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值