关于简单的 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验证实体不同的数据验证组件,提供了将实体与验证分离开来的 ...
随机推荐
- 2019-07-25 PDO
PDO是什么? pdo是php数据对象,即php data object .使用pdo是为了让我们能够使用相同的代码连接不同的数据库.PDO扩展是以面向对象的方式来进行封装,也就是说,我们的PDO扩展 ...
- TCP连接与断开
两台宿主机 一台:作为服务器 另一台:作为客户端 服务器的步骤: 打开[控制面板]----点击[程序]---点击程序和功能中[打开或关闭Windows功能]---点击[Telnet服务器]--点击[确 ...
- 【开发工具】- Java开发必知工具
压力测试工具_JMeter 作用 1.能够对HTTP和FTP服务器进行压力和性能测试, 也可以对任何数据库进行同样的测试(通过JDBC). 2.完全的可移植性和100% 纯java. 3.完全 Swi ...
- js对象及函数(四)
一.对象1.函数对象的创建方法一:使用new构造函数去创建构造函数对象eg: var obj = new Object(); //向对象里面添加属性或方法 obj.name = 'nzc'; obj. ...
- PHP实现智能语音播报
原文地址 https://www.jianshu.com/p/91a046ec6ebc 大家估计都知道现在很多AI音响能够给你播报天气,叫你起床...甚至能够接受语音指令!所谓的人工智能音响,听起来很 ...
- day 20 作业
作业 1.下面这段代码的输出结果将是什么?请解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Parent ...
- oracle instantclient_12_2安装
下载地址 http://www.oracle.com/technetwork/database/database-technologies/instant-client/downloads/index ...
- 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心
2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心 [Proble ...
- BLE——协议层次结构
未完待续…… BLE协议 Bluetooth Application Applications GATT-Based Profiles/Services Bluetooth Core (Stack) ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...