好用的验证框架FluentValidation(上)

2023-11-06 01:18

本文主要是介绍好用的验证框架FluentValidation(上),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

把数据错误扼杀在早期,那就是在数据的入口处,一般数据都是打包成一个实体的方式进传递,FluentValidation就以实体类为单位进行属性验证的集合。

Install-Package FluentValidation

下面看一个例子吧。

实体类:

public class Person
{public int Id { get; set; }public DateTime Birthday { get; set; }public string IDCard { get; set; }public string Name { get; set; }public string Email { get; set; }public PersonAddress Address { get; set; }public string Tel { 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(){RuleFor(p => p.Name).NotNull();RuleFor(p => p.Email).NotNull().EmailAddress();RuleFor(p => p.Birthday).NotNull();RuleFor(p => p.IDCard).NotNull().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}$");RuleFor(p => p.Address).NotNull();RuleFor(p => p.Address).SetValidator(new PersonAddressValidator());}
}
/// <summary>
/// Person Address验证
/// </summary>
public class PersonAddressValidator : AbstractValidator<PersonAddress>
{public PersonAddressValidator(){RuleFor(a => a.Country).NotNull();RuleFor(a => a.Province).NotNull();RuleFor(a => a.City).NotNull();RuleFor(a => a.County).NotNull();RuleFor(a => a.Address).NotNull();RuleFor(a => a.Postcode).NotNull().Length(6);}
}

使用场景:

class Program
{static void Main(string[] args){var person = new Person(){//少一位Tel = "1345346711",Name = "桂素伟",//格式错误Email = "axzxs2001#163.com",//设置生日,没有身份证Birthday = DateTime.Parse("2020-03-28 00:00:00"),Address = new PersonAddress(){//邮编位数不对Postcode = "12345"},};var validator = new PersonValidator();var results = validator.Validate(person);if (!results.IsValid){foreach (var failure in results.Errors){Console.WriteLine("属性 " + failure.PropertyName + " 验证失败:" + failure.ErrorMessage);}}Console.WriteLine("--------------------------------------------------------------------");Console.WriteLine(results.ToString("\r\n"));}
}

FluentValidation有一个很赞的功能,就是验证某一属性时,可以用别的属性的值作为条件,组合实现验证,这样就能适应更多的业务逻辑验证场景。比如上例中的,只有大于一岁(Birthday)的人,身份证(IDCard)是必填项。当然FluentValidation不只这些功能,比如嵌套实体验证,组合验证规则等,都是很贴心的功能,期待大家尝试。

这篇关于好用的验证框架FluentValidation(上)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/353631

相关文章

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

无线路由器哪个品牌好用信号强? 口碑最好的三个路由器大比拼

《无线路由器哪个品牌好用信号强?口碑最好的三个路由器大比拼》不同品牌在信号覆盖、稳定性和易用性等方面各有特色,如何在众多选择中找到最适合自己的那款无线路由器呢?今天推荐三款路由器让你的网速起飞... 今天我们来聊聊那些让网速飞起来的路由器。在这个信息爆炸的时代,一个好路由器简直就是家庭网编程络的心脏。无论你

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

无线领夹麦克风什么牌子好用?揭秘领夹麦克风哪个牌子音质好!

随着短视频行业的星期,围绕着直播和视频拍摄的电子数码类产品也迎来了热销不减的高增长,其中除了数码相机外,最为重要的麦克风也得到了日益增长的高需求,尤其是无线领夹麦克风,近几年可谓是异常火爆。别看小小的一对无线麦克风,它对于视频拍摄的音质起到了极为关键的作用。 不过目前市面上的麦克风品牌种类多到让人眼花缭乱,盲目挑选的话容易踩雷,那么无线领夹麦克风什么牌子好用?今天就给大家推荐几款音质好的

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

Spring框架5 - 容器的扩展功能 (ApplicationContext)

private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");} BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanF

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

数据治理框架-ISO数据治理标准

引言 "数据治理"并不是一个新的概念,国内外有很多组织专注于数据治理理论和实践的研究。目前国际上,主要的数据治理框架有ISO数据治理标准、GDI数据治理框架、DAMA数据治理管理框架等。 ISO数据治理标准 改标准阐述了数据治理的标准、基本原则和数据治理模型,是一套完整的数据治理方法论。 ISO/IEC 38505标准的数据治理方法论的核心内容如下: 数据治理的目标:促进组织高效、合理地

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return