.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(@"^(\d{3,4}-)?\d{6,8}$|^[1]+[3,4,5,8]+\d{9}$").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;
}
}
}
想要更快更方便的了解相关知识,可以关注微信公众号
****欢迎关注我的asp.net core系统课程****
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524