AnsweredAssumed Answered

How can I define optional items in Array Object using Validate JSON Schema?

Question asked by elion on Jun 3, 2016
Latest reply on Jun 8, 2016 by elion

Hi,

 

In this JSON schema, "phoneNumber" is array type.

It has two items. "location" and "code".

"location" is required and "code" is optional item.

And "code" must validate type "integer" if exists.

 

Schema :

{

  "type": "object",

  "properties": {

    "phoneNumber": {

      "type": "array",

      "items": {

        "type": "object",

        "properties": {

          "location": {

            "type": "string"

          },

          "code": {

            "type": "integer"

          }

        },

        "required": [

          "location"

        ]

      }

    }

  },

  "required": [

    "phoneNumber"

  ]

}

 

JSON Data:

{

  "phoneNumber": [

    { "location": "home", "code": 44},

    {"location": "company"}

  ]

}

 

This schema and data is valid in JSON Schema Lint :: JSON Schema Validator .

But is invalid in Validate JSON Schema Assertion.

Error message is "JSON Schema validation failure. $.phoneNumber[1].code: is missing and it is not optional".

 

How to define optional items in array object using Validate JSON Schema assertion?

Outcomes