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

99封情书 提交于 2019-12-13 06:27:50

问题


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. I am using 'ajv' node js template to validate the Json input with template data. 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"
        }
  ]
}`

回答1:


The items keyword takes either a single schema, or an array or schemas.

If you provide an array of schemas, the 1st schema must is applied to the 1st item in the array, and then the 2nd schema is applied to the 2nd item in the array, etc for n schemas and items.

To change your schema so that the value of items is a single schema, wrap your array of schemas in an oneOf. This will mean that each item in the array must be valid for one of the schemas in the array value of oneOf.



来源:https://stackoverflow.com/questions/52713839/how-to-remove-dependencies-of-order-in-json-schema-in-case-of-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!