一般用途pythorn数据验证器
validictor的Python项目详细描述
warning: | :warning: As of 2018 this library is deprecated, please consider using jsonschema (https://pypi.python.org/pypi/jsonschema) instead. |
---|
通用python数据验证器。
适用于Python2.7和Python3.3+
基于json模式建议的模式格式(http://json-schema.org)
包含由Ian Lewis和Yusuke Muraoka从jsonschema派生的代码。
用法
json文档和模式必须首先加载到python字典类型中 在它被验证之前。
解析一个简单的json文档:
>>> import validictory >>> >>> validictory.validate("something", {"type":"string"})
分析更复杂的json文档:
>>> import json >>> import validictory >>> >>> data = json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') >>> schema = { ... "type":"array", ... "items":[ ... {"type":"string"}, ... {"type":"object", ... "properties":{ ... "bar":{ ... "items":[ ... {"type":"string"}, ... {"type":"any"}, ... {"type":"number"}, ... {"type":"integer"} ... ] ... } ... } ... } ... ] ... } >>> validictory.validate(data,schema)
捕获值错误以处理验证问题:
>>> import validictory >>> >>> try: ... validictory.validate("something", {"type":"string","minLength":15}) ... except ValueError, error: ... print(error) ... Length of value 'something' for field '_data' must be greater than or equal to 15
您可以在Read the Docs的官方文档中阅读更多内容。