2

Currently the Schema definition is not documented at all, the only resources i could find are these:

http://support.apiary.io/knowledgebase/articles/147279-json-schema-validation https://github.com/apiaryio/api-blueprint/issues/112

but none of those examples discuss how to create nested schema.

I would like to validate this response:

{
    date: (datetime),
    url: (url),
    changes: [
        {
            date: (datetime),
            url: (url)
        },
        ...
    ],
    items: [
        {
            name: (string),
            url: (url)
        },
        ...
    ]
}

based on my current knowledge, i've started to create schema what looks like this

{
    "type": "object",
    "required": true,
    "properties": {
        "date": {
            "type": "datetime",
            "required": true
        },
        "url": {
            "type": "string",
            "required": true
        },
        "changes": {
            "type": "array",
            "required": true
        },
        "items": {
            "type": "array",
            "required": true
        }
    }
}

but how to create schema for nested item and change?

what types can i use?

how to validate datetime? expected value is YYYY-MM-DD HH:MM:SS

gondo
  • 979
  • 1
  • 10
  • 29
  • ok i've found this https://github.com/Baggz/Amanda/blob/master/docs/json/comparison.md what might be the answer to my question. however this is insane ... – gondo Jan 29 '15 at 18:35

2 Answers2

0

so far i ended up doing this. still i have to test it once my API end point will be up and working

{
    "type": "object",
    "required": true,
    "properties": {
        "date": {
            "type": "string",
            "format": "date-time",
            "required": true
        },
        "url": {
            "type": "string",
            "format": "url",
            "required": true
        },
        "changes": {
            "type": "array",
            "required": true,
            "items": {
                "type": "object",
                "required": true,
                "properties": {
                    "date": {
                        "type": "string",
                        "format": "date-time",
                        "required": true
                    },
                    "url": {
                        "type": "string",
                        "format": "url",
                        "required": true
                    }
                }
            }
        },
        "items": {
            "type": "array",
            "required": true,
            "items": {
                "type": "object",
                "required": true,
                "properties": {
                    "name": {
                        "type": "string",
                        "required": true
                    },
                    "url": {
                        "type": "string",
                        "format": "url",
                        "required": true
                    }
                }
            }
        }
    }
}
gondo
  • 979
  • 1
  • 10
  • 29
0

The test response is the following JSON snippet based on your example. The arrays include two objects so modifications you'd like to test are easier.

{
  "date": "2015-02-05T00:00:00.000Z",
  "url": "mysite.com",
  "changes": [
    {
      "date": "2015-03-05T00:00:00.000Z",
      "url": "mysite.com/edit/1"
    },
    {
      "date": "2015-03-04T00:00:00.000Z",
      "url": "mysite.com/edit/4"
    }
  ],
  "items": [
    {
      "name": "Item One",
      "url": "mysite.com/items/1"
    },
    {
      "name": "Item Two",
      "url": "mysite.com/items/2"
    }
  ]
}

The following schema will validate the above JSON using the draft-04 specification.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Nested Example",
  "type": "object",
  "properties": {
    "date": {
      "type": "string",
      "format": "date-time"
    },
    "url": {
      "type": "string",
      "format": "uri"
    },
    "changes": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "url": {
            "type": "string",
            "format": "uri"
          }
        },
        "required": ["date", "url"]
      }
    },
    "items": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "url": {
            "type": "string",
            "format": "uri"
          }
        },
        "required": ["name", "url"]
      }
    }
  },
  "required": ["date", "url", "changes", "items"]
}

The following types are available:

  • array
  • boolean
  • integer
  • number
  • null
  • object
  • string

Primitive Types


Validation


I haven't had time to research the tools and workflow to do this locally so I've been using this for validation.

slamborne
  • 1,185
  • 11
  • 16