setting required on a json-schema array

ipengineer picture ipengineer · Jul 29, 2013 · Viewed 40.2k times · Source

I am trying to figure out how to set required on my json-schema array of objects. The required property works fine on an object just not an array.

Here is the items part of my json schema:

        "items": {
        "type": "array",
        "properties": {
            "item_id": {"type" : "number"},
            "quantity": {"type": "number"},
            "price": {"type" : "decimal"},
            "title": {"type": "string"},
            "description": {"type": "string"}
        },
        "required": ["item_id","quantity","price","title","description"],
        "additionalProperties" : false
    }

Here is the json array I am sending over. The json validation should fail since I am not passing a description in these items.

       "items": [
        {
            "item_id": 1,
            "quantity": 3,
            "price": 30,
            "title": "item1 new name"
        },
        {
            "item_id": 1,
            "quantity": 16,
            "price": 30,
            "title": "Test Two"
        }
    ]

Answer

theon picture theon · Jul 30, 2013

I got it to work using this validator by nesting the part of the schema for the array elements inside a object with the name items. The schema now has two nested items fields, but that is because one is a keyword in JSONSchema and the other because your JSON actually has a field called items

JSONSchema:

{
   "type":"object",
   "properties":{
      "items":{
         "type":"array",
         "items":{
            "properties":{
               "item_id":{
                  "type":"number"
               },
               "quantity":{
                  "type":"number"
               },
               "price":{
                  "type":"number"
               },
               "title":{
                  "type":"string"
               },
               "description":{
                  "type":"string"
               }
            },
            "required":[
               "item_id",
               "quantity",
               "price",
               "title",
               "description"
            ],
            "additionalProperties":false
         }
      }
   }
}

JSON:

{
   "items":[
      {
         "item_id":1,
         "quantity":3,
         "price":30,
         "title":"item1 new name"
      },
      {
         "item_id":1,
         "quantity":16,
         "price":30,
         "title":"Test Two"
      }
   ]
}

Output with two errors about missing description fields:

[ {
  "level" : "error",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/properties/items/items"
  },
  "instance" : {
    "pointer" : "/items/0"
  },
  "domain" : "validation",
  "keyword" : "required",
  "message" : "missing required property(ies)",
  "required" : [ "description", "item_id", "price", "quantity", "title" ],
  "missing" : [ "description" ]
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/properties/items/items"
  },
  "instance" : {
    "pointer" : "/items/1"
  },
  "domain" : "validation",
  "keyword" : "required",
  "message" : "missing required property(ies)",
  "required" : [ "description", "item_id", "price", "quantity", "title" ],
  "missing" : [ "description" ]
} ]

Try pasting the above into here to see the same output generated.