How to remove dependencies of order in Json schema in case of Array of objects

60 views
Skip to first unread message

Vishu Muthreja

unread,
Oct 12, 2018, 4:17:27 AM10/12/18
to JSON Schema

I have one Json schema template, which contains array of objects. And I need to verify Json input with that template. But I want this should not dependent on order for object in array.

And below we have array of 3 different object in template i.e. abs, endpoint and dispatch. I want to remove dependency of order from here. I can provide ant order of items in Json input schema. It should not dependent on template. Any help would be appreciated. Thanks.

Attached template and input json.

Json Template

var schema1 = {
  "additionalProperties" : {
    "type" : "integer"
  },
    "properties": {
        "name": { "type": "string" },
        "description": { "type": "string" },
        "services": {
            "type": "array",
            "items": [
                {
                    "type": "object",
                    "properties": {
                        "type": { "type": "string", "const" :"abs" },
                        "id": { "type": "string" },
                        "name": { "type": "string" },
                        "appId": { "type": "string" },
                        "endpoint": { "type": "string" }
                    },
                    //"additionalProperties": false
                    "required": [  "type","id", "name","appId", "endpoint"]
                },
                {
                  "type": "object",
                  "properties": {
                      "type": { "type": "string", "const" :"endpoint" },
                      "id": { "type": "string" },
                      "name": { "type": "string" },
                      "appPassword": { "type": "string" },
                      "appId": { "type": "string" },
                      "endpoint": { "type": "string" }
                  },
                  //"additionalProperties": false,
                  "required": [  "type","id", "name","appPassword","appId", "endpoint"]
               }
               ,
               {
                 "type": "object",
                 "properties": {
                     "type": { "type": "string", "const" :"dispatch" },
                     "serviceIds" : {"type":"array", "items": [{ "type": "string" }]},
                     "name": { "type": "string" },
                     "appId": { "type": "string" },
                     "authoringKey": { "type": "string" },
                     "version": { "type": "string" },
                     "region": { "type": "string" },
                     "id": { "type": "string" }
                 },
                 //"additionalProperties": false
                 "required": [  "type","serviceIds", "name","appId","authoringKey","version","region", "id"]
              }
            ]
        }
    }
};


Input Json :

 {
  "name": "ScorpioBot-development",
  "description": "",
  "services": [

      {
            "type": "endpoint",
            "id": "1",
            "name": "development",
            "appId": "test",
            "appPassword": "test",
            "endpoint": "http://localhost:3978"
        },
        {
          "type": "abs",
          "id": "49",
          "name": "test-development",
          "appId": "12323",
          "endpoint": "http://localhost:3978/"
      },
        {
            "type": "endpoint",
            "id": "11",
            "name": "development",
            "appId": "test1",
            "appPassword": "test1",
            "endpoint": "http://localhost:3978"
        },
        {
            "type": "dispatch",
            "serviceIds": [
                "general"
            ],
            "name": "test_Dispatch",
            "appId": "test",
            "authoringKey": "1234,
            "version": "Dispatch",
            "region": "test",
            "id": "dispatch"
        }
  ]
}` {
  "name": "ScorpioBot-development",
  "description": "",
  "services": [

      {
            "type": "endpoint",
            "id": "1",
            "name": "development",
            "appId": "test",
            "appPassword": "test",
            "endpoint": "http://localhost:3978"
        },
        {
          "type": "abs",
          "id": "49",
          "name": "test-development",
          "appId": "12323",
          "endpoint": "http://localhost:3978/"
      },
        {
            "type": "endpoint",
            "id": "11",
            "name": "development",
            "appId": "test1",
            "appPassword": "test1",
            "endpoint": "http://localhost:3978"
        },
        {
            "type": "dispatch",
            "serviceIds": [
                "general"
            ],
            "name": "test_Dispatch",
            "appId": "test",
            "authoringKey": "1234,
            "version": "Dispatch",
            "region": "test",
            "id": "dispatch"
        }
  ]
}`

Henry Andrews

unread,
Oct 18, 2018, 9:30:47 AM10/18/18
to JSON Schema
You can use an "anyOf" or "oneOf" in your array "items", and then set both "minItems" and "maxItems" to 3, and set "uniqueItems" to true.

thanks,
-henry
Reply all
Reply to author
Forward
0 new messages