简单模式验证库

json-checker的Python项目详细描述


BuildCovPython versionsPyPI

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(...)遇到一个类型(例如intstr), 它将检查相应的数据段是否是该类型的实例, 否则它将提高CheckerError

>>>fromjson_checkerimportChecker>>>Checker(int).validate(123)123>>>Checker(int).validate('123')Traceback(mostrecentcalllast):...checker_exceptions.TypeCheckerError:currentvalue'123'(str)isnotint

列表,类似容器

如果Checker(...)遇到listtuplesetfrozenset,它将验证相应数据容器的内容 针对该容器中列出的架构: 如果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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
安卓在Java代码中添加平方根和退格按钮   java H2数据库在创建序列时引发意外的JdbcSQLException   Java解析方法调用其他类   java如何在Android中测量循环中代码的累积执行时间?   javascript如何构造此数组,将类别和产品分开   java正确的时间格式   java可移植性。查找和迭代器。查找而不是引发异常,获取null   Android中Telugu字符串的java正则表达式?   java需要帮助计算arrayList中的字符数   正则表达式Java正则表达式替换双引号内的双引号   安卓 java。lang.unsatifiedLinkError:未找到本机方法:org。pjsip。pjsua。pjsuaJNI。新的伙伴配置:()J   关于Java中线程和回调的多线程问题   java mod_重写规则(如果出现其他问题)   java类导入到jsp并创建对象问题   java正在从mongoDB获取损坏的信息。   java第二个JFrame首先关闭   java如何使JButton接受来自JTextField的输入?   java如何为jacksondataypehal注释记录   Java元编程难题:获取所有本身由给定注释a注释的注释