Package: hehey-hvalidation
hehey-hvalidation介绍hehey-hvalidation 是一个python 全面的验证器工具类。
其主要特点有:支持常用验证器验证器易扩展支持验证字典以及对象属性示例全面依赖以及版本要求python >= 3.5安装直接下载:命令安装：pip install hehey-hvalidation基础文件以目录参数配置# 验证器规则参数ruleConf={"goon":False,# 当验证错误是是否继续验证"message":"你输入的格式错误!",# 错误消息"on":"create",# 使用场景"when":'valint(方法或函数)',# 满足条件};# 验证器参数validatorConf={"message":"你输入的格式错误!",# 错误消息"skipOnEmpty":"create(方法或函数)",# 验证值为空时是否调用验证"emptyFunc":"emptyFunc(方法或函数)",# 验证是否空方法,可以重写"non":False,# 结果是否非"when":'valint()',# 满足条件};基本示例快速使用fromhvalidation.validationimportValidationvalidation=Validation()validateData={'username':'hehe','userid':10,'title':'hehe 每天小酌一杯','age':10,'birthday':'1988-06-14','tel':'13511221111','password':'5241411','confirmpassword':'2541414','sceneType':"login",};validateRules=[['username',[['required'],['alphaNum']],{'message':'请输入的字符必须包含字母字符'}],['userid',[['!empty'],['int']],{'message':'user_id 必须为数字'}],['title',[['required'],['len',{"min":2,"max":200}]],{'message':'请输入一个长度介于{min}和{max}之间的字符串'}],['age',[['required'],['range',{"min":2,"max":200}]],{'message':'请输入一个合法的{min}-{max}数值'}],['birthday',[['required'],['date',{"format":'Y-m-d'}]],{'message':'你输入的日期格式错误,正确日期格式为:{format}'}],['tel',[['required'],['date',{"format":'Y-m-d'}]],{'message':'你的输入手机格式有误,必须11位'}],['password',[['required'],['alphaDash',{"len":[6,15]}]],{'message':'请输入的字符包含字母、数字、破折号（ - ）以及下划线（ _ ）,字符长度在6-15 位'}],['confirmpassword',[['required'],['alphaDash',{"len":[6,15]}],['eqstrfield',{'field':"password"}]],{'message':'你的输入确认密码与密码不一致!'}],['sceneType',[['required'],['enum',{"numbers":['login','changeTel','reg']}],],{'message':'你输入的场景类型错误,必须为{numbers}!'}],];ifnotvalidation.validate(validateData,validateRules):print("验证错误:{0}".format(validation.getFirstError()))else:print('验证通过!')接入hehey 组件# settings.py 配置文件components={'validation':{'clazz':'hvalidation.validation.Validation',},}fromheheimporthevalidateData={'username':'hehe',};validateRules=[['username',[['required'],['alphaNum']],{'message':'请输入的字符必须包含字母字符'}],];ifnothe.app.validation.validate(validateData,validateRules):print("验证错误:{0}".format(he.app.validation.getFirstError()))else:print('验证通过!')多个验证器validateRules=[['title',[['required'],['len',{"min":2,"max":200}]],{'message':'请输入一个长度介于{min}和{max}之间的字符串'}],];验证多个属性validateRules=[[['userid','username'],[['!empty']],{'message':'参数不能为空'}],];验证多维数组validateRules=[[['user.userid'],[['!empty']],{'message':'参数不能为空'}],];多个验证器支持与或 or,and,&,|validateRules=[['username',[['!empty'],['minlen',{'number':1}]],{'message':'请输入一个10-20位的字符串'}],['age',['and',['int'],['minlen',{'number':10}]],{'message':'你的输入的年龄有误,年龄必须超过10 岁！'}],['age1',['or',['int'],['minlen',{'number':10}]],{'message':'你的输入的年龄有误,年龄必须超过10 岁！'}],];设置验证规则错误消息validateRules=[['username',[['!empty']],{'message':'不能为空'}],];设置验证器错误消息validateRules=[['age',[['!empty'],['minlen',{'number':1,"message":"请输入的值必须大于1"}]],{'message':'不能为空'}],];设置验证规则使用场景validateRules=[['username',[['!empty'],['minlen',{'number':1}]],{'message':'请输入一个10-20位的字符串','on':['create','update','login']}],];设置验证规则的前置条件defvalidint(value,name,validator):ifisinstance(value,int):returnTrueelse:returnFalsevalidateRules=[['userid',[['!empty']],{'message':'你输入格式错误','when':validint}],];添加自定义验证器fromhvalidation.validationimportValidationvalidation=Validation()validation.addValidator("ip6",'app.utils.IpValidator.IpValidator','请输入ip格式')validateRules=[['userip',[['!empty'],['ip6']],{'message':'你输入格式错误'}],];验证器直接为方法或函数defvalidint(value,name,validator):ifisinstance(value,int):returnTrueelse:returnFalsevalidateRules=[['userid',[['!empty'],[validint]],{'message':'请输入一个整型数值'}],];直接使用验证器验证fromhvalidation.validationimportValidationvalidation=Validation()# 验证ip6validation.ip('102.021.054.11',mode='ip6');装饰器注册验证器fromhvalidation.validationimportreg_validator# 注册函数方法验证器@reg_validator('用户名不能为空','可定义验证器别名,可不填')defusername(self,value,name=None):ifvalue:returnTrueelse:returnFalse;# 注册类验证器,创建文件IpValidator.pyfromhvalidation.base.ValidatorimportValidatorimportre;@reg_validator('ip 格式有误','ip')classIpValidator(Validator):def__init__(self,attrs,validation=None):self.pattern=r'^(\d+\.\d+\.\d+\.\d+)$'super().__init__(attrs,validation)defvalidateValue(self,value,name=None):ifre.match(self.pattern,value)isnotNone:returnTrueelse:returnFalse装饰器注册验证规则fromhvalidation.validationimportValidation,reg_valid_ruleclassAddress:@reg_valid_rule("ip",property="myuser",attrs={"message":"hehe"})@reg_valid_rule("float",property="myuser",attrs={"message":"hehe"})def__init__(self):self.myuser="什么规则"validation=Validation()address=Address()ifnotvalidation.validate(address):print("验证错误:{0}".format(validation.getFirstError()))else:print('验证通过!')#或ifnotvalidation.validateObject(address):print("验证错误:{0}".format(validation.getFirstError()))else:print('验证通过!')默认验证器验证器说明规则示例required必填字段['fieldname', ['required'] ]empty不为空字段,常配合!使用['fieldname', ['empty'] ]，['fieldname', ['!empty'] ]float数值必须为浮点数,即整型,或带小数点的数值['fieldname', ['float'] ]int数值必须为整型['fieldname', ['int'] ]boolean数值必须为布尔值,True or False['fieldname', ['boolean'] ]tel11 位手机号['fieldname', ['tel'] ]date验证日期格式['fieldname', ['date',{"format":'Y-m-d'}] ]rangedate验证日期范围['fieldname', ['date',{"min":'2019-10-10','max':'2010-10-11'}] ]email验证邮箱格式['fieldname', ['email'] ]ip验证ip格式,支持mode 格式 ip4,ip6['fieldname', ['ip',{"mode":"ip4"}]ip4验证ip4格式['fieldname', ['ip4']]ip6验证ip6格式['fieldname', ['ip6']]url验证url 地址格式,支持设置defaultScheme['fieldname', ['url',{"defaultScheme":"https"}]]range验证数值范围大小['fieldname', ['range',{"min":10,'max':20}] ]compare比较指定数值大小,支持操作符,'gt','egt','lt','elt','eq','>','>=','<','<=','='['fieldname', ['compare',{"number":10,"operator":"gt"}] ]eq验证数值相等,compare 操作符 eq 的简写['fieldname', ['eq',{"number":1}]]gt验证数值大于,compare 操作符 gt 的简写['fieldname', ['gt',{"number":1}]]egt验证数值大于等于,compare 操作符 egt 的简写['fieldname', ['egt',{"number":1}]]lt验证数值小于,compare 操作符 lt 的简写['fieldname', ['lt',{"number":1}]]elt验证数值小于等于,compare 操作符 elt 的简写['fieldname', ['elt',{"number":1}]]minlen验证字符最小长度['fieldname', ['minlen',{"number":1}]]maxlen验证字符最大长度['fieldname', ['maxlen',{"number":1}]]len验证字符最大范围['fieldname', ['maxlen',{"min":1,"max":100}]]currency验证货币数值,比如0.51,支持设置小数点位数decimalPoint['fieldname', ['maxlen',{"decimalPoint":2}]]ch验证中文格式['fieldname', ['ch']]en验证英文格式['fieldname', ['en']]alpha验证包含字母字符[a-z_A-Z]格式['fieldname', ['alpha']]alphaNum验证包含字母、数字格式['fieldname', ['alphaNum']]alphaDash验证字母、数字、破折号（ - ）以及下划线（ _ ）格式['fieldname', ['alphaDash']]inlist输入的值必须包含在指定的列表['fieldname', ['inlist']]enum输入的值必须包含在指定的列表['fieldname', ['enum']]notin输入的值必须不包含在指定的列表['fieldname', ['notin']]vlist验证列表里每个元素的类型['fieldname', ['vlist',[ ['int'],['minlen'] ]]]eqstrfield验证字段与指定字段值是否相等['fieldname', ['eqstrfield',{"field":"confirmpassword"}]]]eqintfield验证字段与指定字段数值是否相等['fieldname', ['eqintfield',{"field":"age"}]]]gtintfield验证字段与指定字段数值是否大于['fieldname', ['gtintfield',{"field":"age"}]]]egtintfield验证字段与指定字段数值是否大于等于['fieldname', ['egtintfield',{"field":"age"}]]]ltintfield验证字段与指定字段数值是否小于['fieldname', ['ltintfield',{"field":"age"}]]]eltintfield验证字段与指定字段数值是否小于等于['fieldname', ['eltintfield',{"field":"age"}]]]gtdatefield验证日期字段与指定日期字段数值是否大于['enddate', ['gtdatefield',{"field":"startdate"}]]]egtdatefield验证日期字段与指定日期字段数值是否大于等于['enddate', ['egtdatefield',{"field":"startdate"}]]]ltdatefield验证日期字段与指定日期字段数值是否小于['enddate', ['ltdatefield',{"field":"startdate"}]]]eltdatefield验证日期字段与指定日期字段数值是否小于等于['enddate', ['eltdatefield',{"field":"startdate"}]]]