• 开源后端数据校验插件Validate.Net,类似Validate.js


    介绍

    Validate.Net是借助Validate.js的思想开发的一套.Net服务端数据校验插件,可以更方便高效的校验实体内的属性值是否合法。内置多种常规数据校验规则(必填、字符串长度区间、字符串最大最小长度、最大最小值、值区间、Email、邮编、url、正整数、负整数、非正整数、非负整数、ip地址、qq、身份证号码、统一社会信用代码,常规密码)。支持自定义正则校验、方法重写等扩展校验。是做后端开发的必备利器。

    软件架构

    .netframework4.5以上、反射、特性

    安装教程

    1. 项目中引用:Validate.Net.dll(文件在目录Validate.NetinRelease下); 
    2. 导入命名空间:using Validate.Net;
    3. gitee地址 https://gitee.com/lkings/Validate.Net
    4. 后续将支持nuget安装

    使用说明

    实体类示例代码

      1 public class UserModel
      2 {
      3     /// <summary>
      4     /// 校验必填,如果添加特性[Describe],则校验该属性不合法时
      5     /// 错误提示信息则是:属性:密码,值:xxxx, 不正确,而不是属性:Password,值:xxxx 不正确
      6     /// </summary>
      7     [ValidateRequired]
      8     [Describe("密码")]
      9     public string Password { get; set; }
     10  
     11     /// <summary>
     12     /// 校验字符串长度区间
     13     /// </summary>
     14     [ValidateRangeLength(6, 20)]
     15     [Describe("用户账户")]
     16     public string Account { get; set; }
     17  
     18     /// <summary>
     19     /// 校验字母,只能包含字母
     20     /// </summary>
     21     [ValidateLetter]
     22     public string Letter { get; set; }
     23  
     24     /// <summary>
     25     /// 校验最大值
     26     /// </summary>
     27     [ValidateMaxValue(20)]
     28     public int Age { get; set; }
     29  
     30     /// <summary>
     31     /// 校验最小值
     32     /// </summary>
     33     [ValidateMinValue(5)]
     34     public int MinLong { get; set; }
     35  
     36     /// <summary>
     37     /// 校验值区间
     38     /// </summary>
     39     [ValidateRangeValue(10,20)]
     40     public int AgeRange { get; set; }
     41  
     42     /// <summary>
     43     /// 校验最大长度
     44     /// </summary>
     45     [ValidateMaxLength(20)]
     46     public string MaxLength { get; set; }
     47  
     48     /// <summary>
     49     /// 校验最小长度
     50     /// </summary>
     51     [ValidateMinLength(5)]
     52     public string MinLength { get; set; }
     53  
     54     /// <summary>
     55     /// 校验Email
     56     /// </summary>
     57     [ValidateEmail]
     58     public string Email { get; set; }
     59  
     60     /// <summary>
     61     /// 校验邮编
     62     /// </summary>
     63     [ValidateZipCode]
     64     public string ZipCode { get; set; }
     65  
     66     /// <summary>
     67     /// 校验包含中文
     68     /// </summary>
     69     [ValidateChinese]
     70     public string Chinese { get; set; }
     71  
     72     /// <summary>
     73     /// 校验负整数
     74     /// </summary>
     75     [ValidateNegtiveInt]
     76     public int NegtiveInt { get; set; }
     77  
     78     /// <summary>
     79     /// 校验正整数
     80     /// </summary>
     81     [ValidatePositiveInt]
     82     public int PositiveInt { get; set; }
     83  
     84     /// <summary>
     85     /// 校验非负整数
     86     /// </summary>
     87     [ValidateNonnegativeInt]
     88     public double NonnegativeInt { get; set; }
     89  
     90     /// <summary>
     91     /// 校验非正整数
     92     /// </summary>
     93     [ValidateNonpositiveInt]
     94     public double NonpositiveInt { get; set; }
     95  
     96     /// <summary>
     97     /// 校验ip地址
     98     /// </summary>
     99     [ValidateIp]
    100     public string Ip { get; set; }
    101  
    102     /// <summary>
    103     /// 校验url
    104     /// </summary>
    105     [ValidateUrl]
    106     public string url { get; set; }
    107  
    108     /// <summary>
    109     /// 校验qq号
    110     /// </summary>
    111     [ValidateQQ]
    112     public string QQ { get; set; }
    113  
    114     /// <summary>
    115     /// 自定义正则校验,特性[ValidateRegular]为自定义正则表达式校验
    116     /// 第一个参数为您的正则表达式,第二个参数为自定义校验不合法时的提示信息
    117     /// </summary>
    118     [ValidateRegular("^[0-9]{6}$", "邮编格式校验失败(自定义)")]
    119     public string CustomStr { get; set; }
    120  
    121     /// <summary>
    122     /// 校验身份证(15位身份证号,和18位身份证号)
    123     /// </summary>
    124     [ValidateIDCard]
    125     public string IDCard { get; set; }
    126  
    127     /// <summary>
    128     /// 校验统一社会信用代码
    129     /// </summary>
    130     [ValidateSocialCreditCode]
    131     public string SocialCreditCode { get; set; }
    132  
    133     /// <summary>
    134     /// 一般密码校验(包含数字和字母及常规特殊符号)
    135     /// </summary>
    136     [ValidatePwd]
    137     public string Pwd { get; set; }
    138 }

    调用示例

     1     static void Main(string[] args)
     2     {
     3         UserModel user = new UserModel()
     4         {
     5             Account = "1000",
     6             Password = "",
     7             Email = "77111.cn",
     8             ZipCode = "6572151",
     9             Chinese = "wjl",
    10             NegtiveInt = 1,
    11             PositiveInt= -2,
    12             Ip = "192.0.0.256",
    13             url = "https://www.baidu.com",
    14             CustomStr = "657215@",
    15             MaxLength = null,
    16             Age = 100,
    17             Letter = "www",
    18             MinLong = 0,
    19             AgeRange = 0,
    20             MinLength = "123456",
    21             QQ = "13155654521",
    22             IDCard = "2102111972110408721",
    23             SocialCreditCode = "91350500611880326T",
    24             Pwd = "wjl1345",
    25             NonpositiveInt = -66.1,
    26             NonnegativeInt = 66.1,
    27         };
    28 
    29         if (!user.Validate<UserModel>(out List<string> errorList))
    30         {
    31             foreach (string error in errorList)
    32             {
    33                 Console.WriteLine($"{error}
    ");
    34             }
    35         }
    36         Console.ReadLine();
    37     }

    执行结果

        属性:密码 - 值:  错误信息: 校验必填项失败

        属性:用户账户 - 值:1000  错误信息: 长度范围为:[6—20]

        属性:Age - 值:100  错误信息: 最大值为:20

        属性:MinLong - 值:0  错误信息: 最小值为:5

        属性:AgeRange - 值:0  错误信息: 值范围为:[10—20]

        属性:Email - 值:77111.cn  错误信息: 邮箱格式校验失败

        属性:ZipCode - 值:6572151  错误信息: 邮政编码格式校验失败

        属性:Chinese - 值:wjl  错误信息: 中文校验失败(必须包含中文)

        属性:NegtiveInt - 值:1  错误信息: 负整数校验失败

        属性:PositiveInt - 值:-2  错误信息: 正整数校验失败

        属性:Ip - 值:192.0.0.256  错误信息: 校验ip失败

        属性:CustomStr - 值:657215@  错误信息: 邮编格式校验失败(自定义)

        属性:IDCard - 值:2102111972110408721  错误信息: 校验身份证合法性失败

        属性:Pwd - 值:wjl1345  错误信息:  一般密码校验失败(包含数字和字母及常规特殊符号)

    gitee地址

     
  • 相关阅读:
    P2082 区间覆盖(加强版)
    Java基础
    @import
    POST方式"Content-type"是"application/x-www-form-urlencoded 的请求遇到的问题
    VBox 安装 macOS 10.12
    对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
    window.location 属性
    post get 区别
    vue 生命周期
    记事本
  • 原文地址:https://www.cnblogs.com/netlws/p/13417476.html
Copyright © 2020-2023  润新知