简单模式验证库
json-checker的Python项目详细描述
json\u checker是用于验证python数据结构的库, 例如从json(或其他)获取到python数据类型的数据。 json_checker有一个参数(soft=true),允许您验证所有json和 在验证完成后引发所有错误,通过api测试是非常有利可图的:
>>>importrequests>>>>>>fromjson_checkerimportChecker>>>>>>>>>deftest_api():>>>res=requests.get(API_URL).json()>>>assertChecker(EXPECTED_RESPONSE,soft=True).validate(res)==res
安装
$ pip install json_checker
示例
下面是一个简单的例子,让您感受一下json\u checker, 使用个人信息验证条目列表:
>>>fromjson_checkerimportChecker>>>current_data={'first_key':1,'second_key':'2'}>>>expected_data={'first_key':int,'second_key':str}>>>checker=Checker(expected_data)>>>result=checker.validate(current_data)>>>assertresult==current_data
如果数据有效,Checker.validate将返回已验证的数据
如果数据无效,Checker将引发CheckerError。
如何json_checker验证数据
类型
如果Checker(...)遇到一个类型(例如int,str), 它将检查相应的数据段是否是该类型的实例, 否则它将提高CheckerError。
>>>fromjson_checkerimportChecker>>>Checker(int).validate(123)123>>>Checker(int).validate('123')Traceback(mostrecentcalllast):...checker_exceptions.TypeCheckerError:currentvalue'123'(str)isnotint
列表,类似容器
如果Checker(...)遇到list,tuple,set或 frozenset,它将验证相应数据容器的内容 针对该容器中列出的架构: 如果paramsoft为true,则验证所有数据, 如果验证后没有有效的数据,则引发异常
>>>Checker([int]).validate([1,1,0,1])[1,1,0,1]>>>Checker([str],soft=True).validate((1,2,3))Traceback(mostrecentcalllast):...checker_exceptions.CheckerError:ListCheckerErrors:currentvalue1(int)isnotstrcurrentvalue2(int)isnotstrcurrentvalue3(int)isnotstr>>>Checker([str]).validate((1,2,3))Traceback(mostrecentcalllast):...checker_exceptions.ListCheckerError:currentvalue1(int)isnotstr
词典
如果Checker(...)遇到dict的实例,它将验证数据 键值对:
>>>current_dict={'first_key':1,'second_key':'2'}>>>checker=Checker({'first_key':int,'second_key':int})>>>checker.validate(current_dict)Traceback(mostrecentcalllast):...checker_exceptions.DictCheckerError:Fromkey="second_key"currentvalue'2'(str)isnotint
运算符或,和,可选键
如果需要验证某些条件中的数据,请使用和运算符 例如,当前数据必须是int instance并且大于0小于99 试试看:
>>>fromjson_checkerimportChecker,And>>>checker=Checker(And(int,lambdax:0<x<99))>>>checker.validate(12)12>>>checker.validate(100)Traceback(mostrecentcalllast):...checker_exceptions.CheckerError:NotvaliddataAnd(int,<lambda>),functionerror
如果需要验证不需要数据值,请使用或运算符 例如,当前数据必须是int或none 试试看:
>>>fromjson_checkerimportChecker,Or>>>checker=Checker(Or(int,None))>>>checker.validate(122)122>>>checker.validate('666')Traceback(mostrecentcalllast):...checker_exceptions.CheckerError:NotvaliddataOr('int',None),currentvalue'666'(str)isnotint,currentvalue'666'(str)isnotNone
如果不需要验证所需的dict键,请使用optionalkey
>>>fromjson_checkerimportChecker,OptionalKey>>>expected_dict={'key1':str,OptionalKey('key2'):int}>>>Checker(expected_dict).validate({'key1':'value'}){'key1':'value'}>>>Checker(expected_dict).validate({'key1':'value','key2':'value2'})Traceback(mostrecentcalllast):...checker_exceptions.DictCheckerError:Fromkey="OptionalKey(key2)"currentvalue'value2'(str)isnotint
更多调试日志
>>>importlogging>>>fromjson_checkerimportChecker>>>logging.basicConfig(level=logging.DEBUG)>>>Checker({'k':str},soft=True).validate({'k':1})DEBUG:json_checker.app:Checkersettings:ignore_extra_keys=False,soft=TrueDEBUG:json_checker.app:DictChecker({'k':<class'str'>} (dict)) start with: {'k': 1}DEBUG:json_checker.app:TypeChecker(str)startwith:1DEBUG:json_checker.app:TypeChecker(str)errorcurrentvalue1(int)isnotstrDEBUG:json_checker.app:DictChecker({'k':<class'str'>} (dict)) error From key="k": current value 1 (int) is not strTraceback(mostrecentcalllast):...CheckerError:Fromkey="k":currentvalue1(int)isnotstr