【AMAD】jsonschema -- (又)一个JSON Schema的Python实现

动机

JSON Schema1是一个专业词汇,可以让你注解验证JSON文档。

使用JSON Schema的好处有:

  • 描述你的数据格式
  • 提供清晰的易读的文档
  • 验证数据:
    • 用户自动化测试
    • 确保用户提交的数据合法

简介

jsonschema2是JSON Schema的python实现。支持python2.7+, python3+.

用法

>>> from jsonschema import validate

>>> # A sample schema, like what we'd get from json.load()
>>> schema = {
...     "type" : "object",
...     "properties" : {
...         "price" : {"type" : "number"},
...         "name" : {"type" : "string"},
...     },
... }

>>> # If no exception is raised by validate(), the instance is valid.
>>> validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema)

>>> validate(
...     instance={"name" : "Eggs", "price" : "Invalid"}, schema=schema,
... )                                   # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
    ...
ValidationError: 'Invalid' is not of type 'number'

个人评分

类型评分
实用性 ⭐️⭐️
易用性 ⭐️⭐️⭐️
有趣性 ⭐️⭐️
posted @ 2019-05-21 18:47  thomaszdxsn  阅读(536)  评论(0编辑  收藏  举报