流畅且易于使用的python数据验证器
onctuous的Python项目详细描述
tl;dr使用volupturous代替
重要
我们挑逗是因为我们提出的问题没有得到解决。 voluputous作者已经解决了这些问题,所以,我们停止了工作 建议任何人改用进化论。
演示文稿
onctuous是一个流畅且易于使用的验证工具 使用。最初基于Voluptuous 由alec thomas编写的代码,我们首先修复了长期悬而未决的问题 比如python内置的冲突和对默认值的支持。
- onctuous的目标是使它简单、平滑。
- 您可以编写自己的验证器
- 您可以指定默认值。最好的?它们不是通过验证所必需的。
- 您可以编写可读代码。这不是基于json模式规范的,是有意的
可以使用onctuous验证list,scalar(常规变量)或 dict。为此,您需要定义一个所谓的Schema和 使用要验证的输入调用架构。如果成功了,它会回来的 已验证的输入,可能根据您的规则进行筛选或编辑
安装
$ pip install onctuous
开发
$ hg clone ssh://hg@bitbucket.org/Ludia/onctuous $ pip install nose nosexcover coverage mock $ python setup.py develop $ nosetests
为什么在另一个验证库上使用onctouus?
它是:
- 可读性
- 简单
- 验证器是简单的可调用函数
- 无需子类化任何东西,只需使用函数即可。
- 错误是简单的异常。
- 验证器只需raise Invalid(msg),就可以期望用户变得有用 消息。
- schemas是基本的python数据结构。
- 您的数据应该是字符串的整数键字典吗?{int: str} 做你想做的事。整数、浮点数或字符串的列表?[int, float, str]。
- 从头设计,不仅用于验证表单。
- 嵌套数据结构的处理方式与任何其他类型相同。需要一个 字典列表?[{}]
- 一致性。
- 架构中的类型被检查为类型。将值作为值进行比较。 调用可调用项进行验证。简单。
示例用法
验证标量
from onctuous import Schema validate_is_int = Schema(int) # Validate 42 (this will run fine) validated = validate_is_int(42) # Validate "toto" (this will raise ``InvalidList`` containing a list of errors) validated = validate_is_int("toto")
验证列表
使用相同的想法,您可以验证int
的列表from onctuous import Schema validate_is_int_list = Schema([int]) # This will run fine validated = validate_is_int_list([42, 2, 7]) # This will raise ``InvalidList`` containing a list of errors validated = validate_is_int_list([2, 7, "toto"])
但是我们也可以使用上的绑定验证器并检查url 例如,要有效,甚至提供自定义错误消息!
from onctuous import Schema, Url validate_is_urls = Schema([Url(msg="Ooops, this is *not* a valid URL")]) # This will run fine validated = validate_is_urls(["www.example.com", "ftp://user:pass@ftp.example.com:42/toto?weird/path"]) # This will raise ``InvalidList`` containing a list of errors validated = validate_is_urls([2, 7, "toto"])
验证字典
同样,这也是一个概念,有更多的细节。例如,这里有一个 基本用户架构:
from onctuous import Schema, Url validate_user = Schema({ 'firstname': unicode, 'lastname': unicode, 'age': int, 'website': Url(msg="Ooops, this is *not* a valid URL"), }) # use it...
但是等等,我不想有太大的年龄,是吗?
from onctuous import Schema, Url, InRange, All validate_user = Schema({ 'firstname': unicode, 'lastname': unicode, 'age': All(int, InRange(min=0, msg="Uh, ages can not be negative...")), 'website': Url(msg="Ooops, this is *not* a valid URL"), }) # use it...
你注意到这是如何使用All来指定int和range 必须满足条件吗?
如果我想让“website”字段成为可选字段怎么办?让我介绍一下Markers
from onctuous import Schema, Url, InRange, All, Optional validate_user = Schema({ 'firstname': unicode, 'lastname': unicode, 'age': All(int, InRange(min=0, msg="Uh, ages can not be negative...")), Optional('website'): Url(msg="Ooops, this is *not* a valid URL"), }) # use it...
您也可以使用带有默认值的“必需”标记。这是非常 如果您不想把全部时间花在写if key in data...上,请使用。
from onctuous import Schema, Url, InRange, All, Required validate_user = Schema({ 'firstname': unicode, 'lastname': unicode, 'age': All(int, InRange(min=0, msg="Uh, ages can not be negative...")), Required('website', "#"): Url(msg="Ooops, this is *not* a valid URL"), }) # use it...
值得注意的是,提供的默认值不需要通过not 验证。在应用程序中,可以将其进一步用作“标记”。
嵌套和高级验证
你可以筑巢。你在上一个例子中用了标量 嵌套到dict或list中。但是你可以任意地将列表嵌套到dict中 另一方面,根据你的需要。
例如,假设你正在写一篇博文,显然有一位作者 可能还有一些标签,长度在3到20个字符之间。
^{公关10}$那都是为了筑巢。
您还可以使用Extra特殊键来允许出现额外的字段 同时仍然有效。
当实例化架构时,还有一个全局的required和extra 可以选择设置的参数。它们都默认为False
进一步
有很多捆绑的验证器,see the full API documentation代表完整的 列表
要求
- Python 2.7.x
- nose, nosexcover, coverage, mock for the tests