动机

验证数据是否符合规范是很有用的,比如:

  • 用于单元测试
  • 用于验证用户提交的数据是否合法

简介

schema1是一个用来验证python数据结构的库。

可以用来验证诸如:

  • 配置文件
  • 表单
  • 外部服务
  • 命令行解析
  • JSON/YAML转换后的数据

用法

这个库相对于jsonschema2,看起来更加的pythonic,也更加通用。后者使用js形式的字符串来注解,在很多idel里面甚至没有高亮提示。

>>> from schema import Schema, And, Use, Optional

>>> schema = Schema([{'name': And(str, len),
... 'age': And(Use(int), lambda n: 18 <= n <= 99),
... Optional('gender'): And(str, Use(str.lower),
... lambda s: s in ('squid', 'kid'))}]) >>> data = [{'name': 'Sue', 'age': '28', 'gender': 'Squid'},
... {'name': 'Sam', 'age': '42'},
... {'name': 'Sacha', 'age': '20', 'gender': 'KID'}] >>> validated = schema.validate(data) >>> assert validated == [{'name': 'Sue', 'age': 28, 'gender': 'squid'},
... {'name': 'Sam', 'age': 42},
... {'name': 'Sacha', 'age' : 20, 'gender': 'kid'}]

个人评分

类型 评分
实用性 ⭐️⭐️⭐️
易用性 ⭐️⭐️⭐️
有趣性 ⭐️⭐️

【AMAD】schema -- 使用pythonic的方式进行schema验证的更多相关文章

  1. 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式

    7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...

  2. onfiguration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...

  3. cvc-elt.1: Cannot find the declaration of element 'beans'Failed to read schema document 'http://www.springframework.org/schema/beans/spring- beans-3.0.xsd'

    Multiple annotations found at this line: - cvc-elt.1: Cannot find the declaration of element 'beans' ...

  4. webServices接口开发过程中项目启动遇到的错误org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 422; schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-bean

    org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 422; schema_reference.4: Failed to read ...

  5. 【Dubbo&&Zookeeper】3、Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法

    转自:http://blog.csdn.net/gaoshanliushui2009/article/details/50469595 我们公司使了阿里的dubbo,但是阿里的开源网站http://c ...

  6. Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法

    Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法 关于dubbo服务的 ...

  7. Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]

    ERROR - Context initialization failed org.springframework.beans.factory.parsing.BeanDefinitionParsin ...

  8. 整合mybatis时报错:Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Una ...

  9. transaction 用tx事务 测试时 报错:Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc]

    Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/sc ...

随机推荐

  1. SpringBoot对接收及返回Instant类型的处理(转)

    一:处理post请求json中的Instant类型1.springboot中日期格式化配置: spring: jackson: date-format: yyyy-MM-dd HH:mm:ss tim ...

  2. 【Winform-自定义控件】自定义控件学习+一个笑脸控件例子

    1.CompositeControls组合控件:在原有控件的基础上根据需要进行组合 2.ExtendedControls 扩展控件:继承自原有控件,添加一些新的属性和方法,绘制一些新元素 当每个But ...

  3. Git 基本操作及命令

    认证部分 1.配置用户信息 git config --global user.name [username] git config --global user.email [email] 2.查询用户 ...

  4. ZOJ - 3715贪心

    ZOJ - 3715KindergartenElection 题目大意:幼儿园里正在举办班长选举,除1号小朋友外每个人都会投他最好的朋友,但1号小朋友可以贿赂别人(小伙子有丶想法),被贿赂的小朋友就会 ...

  5. jeecg中自定义dialog,实现窗体的弹出

    自定一个dialog,在子窗体中写一个方法,然后通过iframe进行调取function createwindowoktext(title, addurl,width,height,oktext,ca ...

  6. AtCoder AGC031D A Sequence of Permutations (群论、置换快速幂)

    题目链接 https://atcoder.jp/contests/agc031/tasks/agc031_d 题解 这居然真的是个找规律神题... 首先要明白置换的一些基本定义,置换\(p\)和\(q ...

  7. UCenter网站部署

    搭建LAMP环境部署UCenter LAMP环境: Linux+Apache+mysql+php  用来搭建动态网站或者服务器的开源软件 一.需要安装的软件 [root@tiandong ~]# yu ...

  8. brute爆破

    0X01明文传输的表单爆破用户名和密码 不存在任何加密 直接爆破即可 当不存在用户名时: 当存在用户名时,密码错误: 这里由于靶场关了 所以我们用dvwa演示 但是dvwa没有以上的差别 所以我们默认 ...

  9. Requests 代理池

    Requests 本身不提供代理池,然而爬数据又要用,所以只能自己搞.其实还挺简单的.我也不知道为什么这么有用的 feature 一直没有被加入. import requests class Clie ...

  10. Mysql 修改密码和设置远程连接

    [参考文章]:mysql修改root密码和设置权限 1. 修改密码 1.1 set password 登录mysql set password for 用户名@localhost = password ...