跳转到主要内容

JSON架构定义辅助工具

项目描述

辅助您定义用于验证或发布的JSON架构。

需求

它需要Python 2.7和jsonschema。jsonschema或setuptools应与Python一起安装。

安装

使用pip

pip install schemabuilder

或easy_install

easty_install schemabuilder

您可以手动安装它

git clone https://github.com/dinoboff/schemabuilder.git
cd schemabuilder
python setup.py install

用法

原语

JSON架构原语由类型为

  • schemabuilder.Str的对象表示

  • schemabuilder.Bool

  • schemabuilder.Number

  • schemabuilder.Int

  • schemabuilder.Object

  • schemabuilder.Array

>>> import schemabuilder as jsb
>>> import pprint
>>>
>>> name = jsb.Str(pattern="^[a-zA-Z][- 'a-zA-Z0-9]+")
>>> email = jsb.Str(format="email")
>>> user = jsb.Object(properties={
...   'name': name(required=True),
...   'email': email(),
...   'home': jsb.Str(format='uri'),
... })
>>> pprint.pprint(user.to_dict())
{'properties': {'email': {'type': 'string'},
                'home': {'format': 'uri', 'type': 'string'},
                'name': {'type': 'string'}},
 'required': ['name'],
 'type': 'object'}

架构

架构收集那些定义以进行验证(使用jsonschema)或发布。

>>> import schemabuilder as jsb
>>> import pprint
>>>
>>> my_schemas = jsb.Schema(id='http://example.com/schemas.json#')
>>> name = my_schemas.define(
...   'name', jsb.Str(pattern="^[a-zA-Z][- 'a-zA-Z0-9]+")
... )
>>> email = my_schemas.define('email', jsb.Str(format="email"))
>>> user = my_schemas.define('user', jsb.Object(properties={
...   'name': name(required=True),
...   'email': email(required=True),
... }))
>>>
>>> user.validate({'name': 'bob'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "schemabuilder/schema.py", line 50, in validate
    validator.validate(data)
  File "/Users/bob/pyenv/lib/python2.7/site-packages/jsonschema/validators.py", line 117, in validate
    raise error
jsonschema.exceptions.ValidationError: 'email' is a required property

Failed validating 'required' in schema:
    {'properties': {'email': {'$ref': '#/definitions/email'},
                    'name': {'$ref': '#/definitions/name'}},
     'required': ['name', 'email'],
     'type': 'object'}

On instance:
    {'name': 'bob'}
>>>
>>> user.validate({'name': 'bob', 'email': 'bob@example.com'})
>>>
>>> import json
>>> print json.dumps(my_schemas.to_dict(), indent=4)
{
    "definitions": {
        "email": {
            "type": "string",
            "format": "email"
        },
        "user": {
            "required": [
                "name",
                "email"
            ],
            "type": "object",
            "properties": {
                "name": {
                    "$ref": "#/definitions/name"
                },
                "email": {
                    "$ref": "#/definitions/email"
                }
            }
        },
        "name": {
            "pattern": "^[a-zA-Z][- 'a-zA-Z0-9]+",
            "type": "string"
        }
    },
    "id": "http://example.com/schemas.json#",
    "$schema": "https://json-schema.fullstack.org.cn/draft-04/schema#"
}

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

schemabuilder-0.3.0.tar.gz (7.1 kB 查看哈希值)

支持