.NET6之MiniAPI(二十):实体验证FluentValidation
Posted dotNET跨平台
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.NET6之MiniAPI(二十):实体验证FluentValidation相关的知识,希望对你有一定的参考价值。
为了验证api post上来的数据的有效性,我们可以引入FluentValidation(详见https://fluentvalidation.net)。在asp.net mvc中,使用的是模型验证,通过在实体类上添加特性达到验证效果。
FluentValidation的原理是通过实现AbstractValidator<T>来实现对T实体类的验证,通过不同折Rule来验证T中的属性(更多验证规则 ,参见官网),见如下实现:
public class Person
public int Id get; set;
public string Name get; set;
public string Tel get; set;
public string Email get; set;
public DateTime Birthday get; set;
public string IDCard get; set;
public PersonAddress Address get; set;
public class PersonAddress
public string Country get; set;
public string Province get; set;
public string City get; set;
public string County get; set;
public string Address get; set;
public string Postcode get; set;
/// <summary>
/// Person验证
/// </summary>
public class PersonValidator : AbstractValidator<Person>
public PersonValidator(IPersonService personService)
RuleFor(p => p.Name).NotNull().NotEmpty();
RuleFor(p => p.Email).NotNull().EmailAddress();
RuleFor(p => p.Birthday).NotNull();
RuleFor(p => p.IDCard)
.NotNull()
.NotEmpty()
.Length(18)
.When(p => (DateTime.Now > p.Birthday.AddYears(1)))
.WithMessage(p => $"出生日期为p.Birthday,现在时间为DateTime.Now,大于一岁,CardID值必填!");
RuleFor(p => p.Tel).NotNull().Matches(@"^(\\d3,4-)?\\d6,8$|^[1]+[3,4,5,8]+\\d9$").WithMessage("电话格式为:0000-0000000或13000000000");
RuleFor(p => p.Address).NotNull();
RuleFor(p => p.Address).SetValidator(new PersonAddressValidator());
//通过调用外部方法来验证
RuleFor(p => p.Id).Must(id => personService.IsExist(id)).WithMessage(p => $"不存在id=p.Id的用户");
/// <summary>
/// Person Address验证
/// </summary>
public class PersonAddressValidator : AbstractValidator<PersonAddress>
public PersonAddressValidator()
RuleFor(a => a.Country).NotNull().NotEmpty();
RuleFor(a => a.Province).NotNull().NotEmpty();
RuleFor(a => a.City).NotNull().NotEmpty();
RuleFor(a => a.County).NotNull().NotEmpty();
RuleFor(a => a.Address).NotNull().NotEmpty();
RuleFor(a => a.Postcode).NotNull().NotEmpty().Length(6);
在mini api引入FluentValidation也很简单,可以通过注入IValidator<T>实现,也可以注放AddFluentValidation,用IValidatorFactory来获取Validator来实现验证,代码如下:
using FluentValidation;
using FluentValidation.AspNetCore;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddFluentValidation();
builder.Services.AddScoped<IValidator<Person>, PersonValidator>();
builder.Services.AddScoped<IPersonService, PersonService>();
var app = builder.Build();
app.MapPost("/person", async (IValidator<Person> validator, Person person) =>
var result = await validator.ValidateAsync(person);
if (!result.IsValid)
var errors = new StringBuilder();
foreach (var valid in result.Errors)
errors.AppendLine(valid.ErrorMessage);
return errors.ToString();
return "OK";
);
app.MapPost("/person1", async (IValidatorFactory validatorFactory, Person person) =>
var result = await validatorFactory.GetValidator<Person>().ValidateAsync(person);
if (!result.IsValid)
var errors = new StringBuilder();
foreach (var valid in result.Errors)
errors.AppendLine(valid.ErrorMessage);
return errors.ToString();
return "OK";
);
app.MapPost("/person2", async (IValidatorFactory validatorFactory, Person person) =>
var result = await validatorFactory.GetValidator(typeof(Person)).ValidateAsync(new ValidationContext<Person>(person));
if (!result.IsValid)
var errors = new StringBuilder();
foreach (var valid in result.Errors)
errors.AppendLine(valid.ErrorMessage);
return errors.ToString();
return "OK";
);
app.Run();
public interface IPersonService
public bool IsExist(int id);
public class PersonService : IPersonService
public bool IsExist(int id)
if (DateTime.Now.Second % 2 == 0)
return false;
else
return true;
以上是关于.NET6之MiniAPI(二十):实体验证FluentValidation的主要内容,如果未能解决你的问题,请参考以下文章