Layer7 API Management

Expand all | Collapse all

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

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

    Posted Jun 03, 2016 02:32 AM

    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?



  • 2.  Re: How can I define optional items in Array Object using Validate JSON Schema?
    Best Answer

    Posted Jun 03, 2016 09:45 AM

    The issue here is that the Gateway only supports JSON Schema v2 and the use of "required" was introduced in JSON Schema v4(Which is what that validator website uses by default). See the following community post for information on using "optional" instead...

    How to define schema using Validate JSON Schema Assertion?



  • 3.  Re: How can I define optional items in Array Object using Validate JSON Schema?

    Posted Jun 07, 2016 09:11 PM

    It works very well. Thanks:)