关于简单的 FluentValidation 验证
FluentValidation : https://github.com/JeremySkinner/FluentValidation
关于为何要使用,因为微软自带的模型验证有点弱,还需要自己去写一大堆的验证。
关于asp.net core的集成 我用的是 FluentValidation.AspNetCore nuget
直接在addmvc后添加 AddFluentValidation() 就好了
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddFluentValidation();
我一般用反射注入msdi
// 注册 Validator
var types = assembly.GetTypes().Where(p =>
p.BaseType != null && p.BaseType.GetInterfaces().Any(x => x == typeof(IValidator)));
foreach (var type in types)
{
if (type.BaseType != null)
{
var genericType = typeof(IValidator<>).MakeGenericType(type.BaseType.GenericTypeArguments[]);
services.AddTransient(genericType, type);
}
}
然后这里例举一些比较常用的方法
以下是我的模型 。
public class UserInput
{
public string CustomerId { get; set; }
public string UserName { get; set; }
public string PhoneNumber { get; set; }
public string FullName { get; set; }
public string Description { get; set; }
public string UnionId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
这里是我的validator
public class UserInputVaildator : AbstractValidator<UserInput>
{
public UserInputVaildator()
{
RuleFor(m => m.UserName).NotEmpty().WithMessage("登录名是必须的"); RuleFor(m => m.CustomerId).NotEmpty().WithMessage("客户Id是必须的");
//当手机号为空的时候就不会再去验证手机号格式是否 因为默认是不会停止验证。
RuleFor(m => m.PhoneNumber)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("手机号是必须的")
.PhoneNumber().WithMessage("手机格式不正确");
//这里的意思是当 邮箱不为空时采取验证邮箱格式是否正确
RuleFor(m => m.Email)
.EmailAddress().When(m => !string.IsNullOrWhiteSpace(m.Email)).WithMessage("邮箱格式不正确");
}
}
当然,还有一些其他的东西
public class TestInput
{
public string Grant { get; set; } public int Number { get; set; }
}
public class TestInputValidator : AbstractValidator<TestInput>
{
public TestInputValidator()
{
RuleSet("test", () =>
{
RuleFor(m => m.Number).GreaterThan().WithMessage("Number要大于0");
});
RuleFor(m => m.Grant).NotEmpty().WithMessage("Grant不能为空");
}
}
规则的设置,可以适应不同的验证场景,对同一个Model进行不同的验证
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
//
[HttpGet]
public IActionResult Get([FromQuery]TestInput input)
{
return Ok();
}
//规则的设置,可以适应不同的验证场景,对同一个Model进行不同的验证
[HttpPost]
public IActionResult Index([FromBody][CustomizeValidator(RuleSet = "test")]TestInput input)
{
return Ok("");
}
}
父类,接口的验证
public class CustomerCreateInput : ClientInput, ICustomerInput{
//具体的实现接口
}
public class CustomerInputInterfaceValidator : AbstractValidator<ICustomerInput>{
//具体 接口 验证逻辑
}
public class ClientInputVaildators : AbstractValidator<ClientInput>{
//集体 基类 验证逻辑
}
//那么我该如果重用接口和基类的验证逻辑
public class CustomerCreateInputValidator : AbstractValidator<CustomerCreateInput>
{
public CustomerCreateInputValidator()
{
//只需要包含进来
Include(new CustomerInputInterfaceValidator());
Include(new ClientInputVaildators());
}
}
这里就有个问题,如果包含的验证类中包含了 RuleSet,那么该如何启用。因为默认不会启用,这个问题,我也不知道 (~ ̄▽ ̄)~ 只能说我也不太精通,昨天刚刚开始用。
关于简单的 FluentValidation 验证的更多相关文章
- .NET平台开源项目速览(10)FluentValidation验证组件深入使用(二)
在上一篇文章:.NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(一) 中,给大家初步介绍了一下FluentValidation验证组件的使用情况.文章从构建间的验证器开 ...
- .NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(一)
在文章:这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,给大家初步介绍了一下FluentValidation验证组件.那里只是概述了一下,并没有对其使用和强大功能做深入研究 ...
- NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(转载)
原文地址:http://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_FluentValidation_1.html 阅读目录 1.基本介绍 ...
- ASP.NET MVC中使用FluentValidation验证实体
1.FluentValidation介绍 FluentValidation是与ASP.NET DataAnnotataion Attribute验证实体不同的数据验证组件,提供了将实体与验证分离开来的 ...
- jQuery validate 根据 asp.net MVC的验证提取简单快捷的验证方式(jquery.validate.unobtrusive.js)
最近在学习asp.netMVC,发现其中的验证方式书写方便快捷,应用简单,易学好懂. 验证方式基于jQuery的validate 验证方式,也可以说是对jQuery validate的验证方式的扩展, ...
- shiro 简单的身份验证 案例
Apache Shiro是Java的一个安全框架,Shiro可以帮助我们完成:认证.授权.加密.会话管理.与Web集成.缓存等. 简单的身份验证 项目目录: 首先,在shiro.ini里配置了用户名和 ...
- jQuery结合Ajax实现简单的前端验证和服务端查询
上篇文章写了简单的前端验证由传统的JavaScript转向流畅的jQuery滑动验证,现在拓展一下,使用Ajax实现用户体验比较好的异步查询,同样还是从建立一个简单的表单开始 <form nam ...
- cookie小栗子-实现简单的身份验证
关于Cookie Cookie是一种能够让网站Web服务器把少量数据储存到客户端的硬盘或内存里,或是从客户端的硬盘里读取数据的一种技术. 用来保存客户浏览器请求服务器页面的请求信息,可以在HTTP返回 ...
- ASP.NET MVC中使用FluentValidation验证实体(转载)
1.FluentValidation介绍 FluentValidation是与ASP.NET DataAnnotataion Attribute验证实体不同的数据验证组件,提供了将实体与验证分离开来的 ...
随机推荐
- Python 第三方日志框架loguru使用
解决中文乱码问题 项目地址 github: https://github.com/Delgan/loguru 文档:https://loguru.readthedocs.io/en/stable/in ...
- 纽约LangeEylandt长岛LongIsland
LangeEylandt n.长岛(美国) 纽约长岛 纽约长岛 (LongIsland)是北美洲在大西洋内的一个岛,最早追溯到十七世纪的1650年被命名为Lange Eylandt [1] ,位于北美 ...
- python多任务的实现:线程,进程,协程
什么叫“多任务”呢?简单地说,就是操作系统可以同时运行多个任务.打个比方,你一边在用浏览器上网,一边在听MP3,一边在用Word赶作业,这就是多任务,至少同时有3个任务正在运行.还有很多任务悄悄地在后 ...
- 【转】StackTraceElement获取方法调用栈的信息
本文链接:https://blog.csdn.net/hp910315/article/details/52702199 一.什么是StackTrace StackTrace(堆栈轨迹)存放的就是方法 ...
- day 19 作业
今日作业 1.什么是对象?什么是类? 对象是特征与技能的结合体,类是一系列对象相同的特征与技能的结合体 2.绑定方法的有什么特点 由对象来调用称之为对象的绑定方法,不同的对象调用该绑定方法,则会将不同 ...
- day 04 预科
目录 变量 什么是变量 变量的组成 变量名的命名规范 注释 单行注释 多行注释 turtle库的使用 今日内容 数据类型基础 变量 具体的值 存不是目的,取才是目的 为了描述世界万物的状态,因此有了数 ...
- HTML&CSS基础-标签的属性
HTML&CSS基础-标签的属性 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.html源代码 <!-- html 根标签,一个页面中有且只有一个根标签,网页中的 ...
- yum仓库的部署
https://segmentfault.com/a/1190000013968371 私有yum仓库在企业中的应用还是比较广泛,有方便.快捷.灵活等优势.如某公司安全部门不允许大批量的主机连接互联网 ...
- 关闭firefox火狐浏览器下载完成时自动扫描(49.0.2以后版本)
本人自己找到的方法,亲测有效,如下:1.在火狐浏览器地址里输入about:config回车,可能会提示“这可能使质量保证失效”,点击[我了解此风险!]2.在搜索框里输入browser.safebrow ...
- Windows安装Redis(转!)
转自https://www.cnblogs.com/wxjnew/p/9160855.html “现在我已经走到了人生的十字路口边了,我相信,在已走过的人生道路中,我一直知道其中哪一条是正确的,是的, ...