动机

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

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

简介

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. MySQL 关于索引的操作

    -- 索引分类? 1.普通索引 2.唯一索引 3.全文索引 4.组合索引 普通索引:仅加速查询,最基本的索引,没有任何限制 唯一索引:加速查询 + 列值唯一(可以有null) 全文索引:仅适用于MyI ...

  2. SQLite3的安装与使用

    下载地址:https://www.sqlite.org/download.html (下载相对应自已电脑的配置的数据库)(这里 我的电脑是 windows 64位操作系统) 下载完后 解压出来 sql ...

  3. shell的正则表达式

    正则表达式处理文件的内容,shell处理文件本身 grep *匹配0到n个 .(点儿)能匹配任意字符----8.8.8.8用于测试外网是否通畅 egrep

  4. 32. ClustrixDB License管理

    一.许可的概述 ClustrixDB必须拥有有效的许可证才能运行.本授权指定: 集群中允许的最大节点数 ClustrixDB将使用的最大核数 在裸金属系统上,ClustrixDB将尝试启用与已授权的物 ...

  5. 利用栈实现字符串中三种括号的匹配问题c++语言实现

    编写一个算法,检查一个程序中的花括号,方括号和圆括号是否配对,若能够全部配对则返回1,否则返回0. Head.h: #ifndef HEAD_H_INCLUDED #define HEAD_H_INC ...

  6. 『HGOI 20190917』Cruise 题解 (计算几何+DP)

    题目概述 在平面直角坐标系的第$1$象限和第$4$象限有$n$个点,其中第$i$个点的坐标为$(x_i,y_i)$,有一个权值$p_i$ 从原点$O(0,0)$出发,不重复的经过一些点,最终走到原点, ...

  7. typedef简化

    /*** mystrcat: ***/ #include<stdio.h> #include<string.h> char *mystrcat(char *s1,char *s ...

  8. 关于brew没有搜索到php的解决方案

    在终端添加php的资源包 brew tap homebrew/homebrew-php 链接 https://github.com/Homebrew/homebrew-php

  9. Idea关于Lombok的一些问题( java: 找不到符号 符号)

    22:15 Lombok Requires Annotation Processing Annotation processing seems to be disabled for the proje ...

  10. 软工第04组 Alpha冲刺(1/6)

    队名:new game 组长博客:戳 作业博客:戳 组员情况 鲍子涵(队长) 过去一段时间对项目的精度和分工进行了更加细致的划分,并初步进行了GamePlay逻辑部分的框架设计 GitHub签入记录: ...