关于简单的 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验证实体不同的数据验证组件,提供了将实体与验证分离开来的 ...
随机推荐
- MySQL路线
一 数据库简介与安装 二 库操作 三 表操作 四 数据操作 五 索引原理与慢查询优化 六 数据备份与慢查询优化 七 视图.触发器.事务.存储过程.函数
- 【转载】 C#中使用float.Parse方法将字符串转换为Float类型
在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为单精度Float类型就是一个常见的类型转换操作,float.Parse方法是C#中专门用来将字符串转换为float类型的,f ...
- Charles关于Https SSLHandshake解决备忘录
抓包Https时错误提示:SSLHandshake: Received fatal alert: unknown_ca 1.准备工作,下载Charles版本 有情链接,提取码为:ghc6,其中包含 ...
- [Props] vue组件间的传值及校验
基本用法 Prop的基本用法很简单,只需要在子组件的Vue实例中定义该属性并把值设为目标属性的数组即可 Vue.component('child', { ... // 接收message props: ...
- unity读取Texture文件并转为Sprit
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; usin ...
- Java文件流下载并提示文件不存在
做文件下载功能的时候,一般使用流的形式下载文件, 如果源文件不存在,下载页面可能就会没有提示,或者一片空白 用户操作之后可能一头雾水,那如何友好提示呢? 想到的有两种 1.可以尝试下载一个名称为:文件 ...
- MySQL NULL--三值逻辑(Three Value Logic)
三值逻辑(Three Value Logic) 在关系型数据库中,由于NULL值的存在,导致逻辑表达式存在三种值:TRUE/FALSE/UNKNOW. SELECT '=NULL AS C1, ' A ...
- 函数式接口(Functional Interface)
原文链接:https://www.cnblogs.com/runningTurtle/p/7092632.html 阅读目录 什么是函数式接口(Functional Interface) 函数式接口用 ...
- 基于gtk的imshow:用gtk读取并显示图像
gtk实现imshow,最naive的做法是用gtk的组件去读取图像,然后show出来:后续再考虑用GTK显示用别的方式例如stb image读取的图像.先前基于GDI实现imshow时也是这一思路, ...
- Linux指令(搜索查找类)
find指令 将从指定目录下递归的遍历其各个子目录,将满足条件的文件或者目录显示在终端. 基本语法: find [搜索范围] [选项] 选项说明: 选项 功能 -name<查询方式> 按照 ...