Skip to content

Instantly share code, notes, and snippets.

@zhangcheng
Created October 16, 2013 06:31
Show Gist options
  • Save zhangcheng/7003452 to your computer and use it in GitHub Desktop.
Save zhangcheng/7003452 to your computer and use it in GitHub Desktop.

Revisions

  1. zhangcheng created this gist Oct 16, 2013.
    67 changes: 67 additions & 0 deletions schema.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    import json
    from jsonschema import validate


    str_schema = '''
    {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product set",
    "type": "array",
    "items": {
    "title": "Product",
    "type": "object",
    "properties": {
    "id": {
    "description": "The unique identifier for a product",
    "type": "number"
    },
    "name": {
    "type": "string"
    }
    },
    "required": ["id", "name"]
    }
    }
    '''

    str_p1 = '''
    [
    {
    "id": 2,
    "name": "An ice sculpture"
    }
    ]
    '''

    str_p2 = '''
    [
    {
    "id": 1,
    "name": "An ice"
    },
    {
    "id": 2,
    "name": "An ice sculpture"
    }
    ]
    '''

    str_p3 = '''
    [
    {
    "id": 2,
    "name2": "An ice sculpture"
    }
    ]
    '''


    schema = json.loads(str_schema)
    p1 = json.loads(str_p1)
    validate(p1, schema)

    p2 = json.loads(str_p2)
    validate(p2, schema)

    p3 = json.loads(str_p3)
    validate(p3, schema)