How to remove dependencies of order in Json schema

2019-08-26 23:29发布

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条回答
Animai°情兽
2楼-- · 2019-08-26 23:54

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.

查看更多
登录 后发表回答