Maihan Nijat
Maihan Nijat

Reputation: 9355

How to use JSON schema validation against JSON document having arrays?

I have json document which contains three main elements along with sub elements. These elements also have arrays. I validated this document with json schema but I am not sure if there is any simple way to get rid of repeating schema for arrays as the current schema is too long.

JSON Schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "/",
  "type": "object",
  "properties": {
    "book": {
      "id": "book",
      "type": "array",
      "items": [
        {
          "id": "0",
          "type": "object",
          "properties": {
            "isbn": {
              "id": "isbn",
              "type": "string"
            },
            "title": {
              "id": "title",
              "type": "string"
            },
            "price": {
              "id": "price",
              "type": "string"
            },
            "categories": {
              "id": "categories",
              "type": "array",
              "items": [
                {
                  "id": "0",
                  "type": "object",
                  "properties": {
                    "category": {
                      "id": "category",
                      "type": "string"
                    }
                  }
                },
                {
                  "id": "1",
                  "type": "object",
                  "properties": {
                    "category": {
                      "id": "category",
                      "type": "string"
                    }
                  }
                },
                {
                  "id": "2",
                  "type": "object",
                  "properties": {
                    "category": {
                      "id": "category",
                      "type": "string"
                    }
                  }
                }
              ]
            },
            "warehouse": {
              "id": "warehouse",
              "type": "object",
              "properties": {
                "location": {
                  "id": "location",
                  "type": "string"
                },
                "aisle": {
                  "id": "aisle",
                  "type": "string"
                },
                "shelf": {
                  "id": "shelf",
                  "type": "string"
                }
              }
            }
          },
          "required": [
            "isbn",
            "title",
            "price",
            "categories",
            "category",
            "warehouse"
          ]
        },
        {
          "id": "1",
          "type": "object",
          "properties": {
            "isbn": {
              "id": "isbn",
              "type": "string"
            },
            "title": {
              "id": "title",
              "type": "string"
            },
            "price": {
              "id": "price",
              "type": "string"
            },
            "categories": {
              "id": "categories",
              "type": "array",
              "items": [
                {
                  "id": "0",
                  "type": "object",
                  "properties": {
                    "category": {
                      "id": "category",
                      "type": "string"
                    }
                  }
                },
                {
                  "id": "1",
                  "type": "object",
                  "properties": {
                    "category": {
                      "id": "category",
                      "type": "string"
                    }
                  }
                },
                {
                  "id": "2",
                  "type": "object",
                  "properties": {
                    "category": {
                      "id": "category",
                      "type": "string"
                    }
                  }
                }
              ]
            },
            "warehouse": {
              "id": "warehouse",
              "type": "object",
              "properties": {
                "location": {
                  "id": "location",
                  "type": "string"
                },
                "aisle": {
                  "id": "aisle",
                  "type": "string"
                },
                "shelf": {
                  "id": "shelf",
                  "type": "string"
                }
              }
            }
          }
        },
        {
          "id": "2",
          "type": "object",
          "properties": {
            "isbn": {
              "id": "isbn",
              "type": "string"
            },
            "title": {
              "id": "title",
              "type": "string"
            },
            "price": {
              "id": "price",
              "type": "string"
            },
            "categories": {
              "id": "categories",
              "type": "array",
              "items": [
                {
                  "id": "0",
                  "type": "object",
                  "properties": {
                    "category": {
                      "id": "category",
                      "type": "string"
                    }
                  }
                },
                {
                  "id": "1",
                  "type": "object",
                  "properties": {
                    "category": {
                      "id": "category",
                      "type": "string"
                    }
                  }
                },
                {
                  "id": "2",
                  "type": "object",
                  "properties": {
                    "category": {
                      "id": "category",
                      "type": "string"
                    }
                  }
                }
              ]
            },
            "warehouse": {
              "id": "warehouse",
              "type": "object",
              "properties": {
                "location": {
                  "id": "location",
                  "type": "string"
                },
                "aisle": {
                  "id": "aisle",
                  "type": "string"
                },
                "shelf": {
                  "id": "shelf",
                  "type": "string"
                }
              }
            }
          }
        }
      ],
      "required": [
        "0",
        "1",
        "2"
      ]
    }
  },
  "required": [
    "book"
  ]
}

And JSON document:

{  
   "book":[  
      {  
         "isbn":"0-672-33751-7",
         "title":"Unity Game Development",
         "price":"$55.99",
         "categories":[  
            {  
               "category":"Game Development"
            },
            {  
               "category":"Unit Game Engine"
            },
            {  
               "category":"Beginner to Intermediate"
            }
         ],
         "warehouse":{  
            "location":"North Warehouse",
            "aisle":"A16",
            "shelf":"3"
         }
      },
      {  
         "isbn":"978-0-9871530-7-4",
         "title":"Jquery: Novice to Ninja",
         "price":"$39.95",
         "categories":[  
            {  
               "category":"JavaScript"
            },
            {  
               "category":"jQuery"
            },
            {  
               "category":"Web Development"
            }
         ],
         "warehouse":{  
            "location":"North Warehouse",
            "aisle":"W03",
            "shelf":"6"
         }
      },
      {  
         "isbn":"0-538-74477-4",
         "title":"Programming Logic and Design",
         "price":"$89.99",
         "categories":[  
            {  
               "category":"Programming"
            },
            {  
               "category":"JavaScript"
            },
            {  
               "category":"Computer Logic"
            }
         ],
         "warehouse":{  
            "location":"South Warehouse",
            "aisle":"B44",
            "shelf":"1"
         }
      }
   ]
}

Upvotes: 0

Views: 85

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24489

You've got a lot of issues with this schema. I suggest you read http://spacetelescope.github.io/understanding-json-schema/ to get a better understanding of JSON Schema. Below is the corrected schema. I'll give some brief advice, but not a lot of explanation. Go read that link for more info.

  1. id is required to be a absolute URL. "/" is not valid
  2. Don't use id anywhere other than the root of the document. It has some unexpected properties. It's best to just avoid it.
  3. The items keyword should be a schema not an array of schemas. The form you are using is not intended for arrays with a single type of item.
  4. The required keyword applies only to properties in the schema it appears in. Sub-schemas need to define their own required properties.
  5. The required keyword does not work on arrays. Use minItems and maxItems instead.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "book": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "isbn": { "type": "string" },
          "title": { "type": "string" },
          "price": { "type": "string" },
          "categories": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "category": { "type": "string" }
              },
              "required": ["category"]
            }
          },
          "warehouse": {
            "type": "object",
            "properties": {
              "location": { "type": "string" },
              "aisle": { "type": "string" },
              "shelf": { "type": "string" }
            }
          }
        },
        "required": [
          "isbn", "title", "price", "categories", "warehouse"
        ]
      }
    }
  },
  "required": ["book"]
}

Upvotes: 1

Related Questions