在对象数组的情况下如何删除Json模式中顺序的依赖性 [英] How to remove dependencies of order in Json schema in case of Array of objects

查看:88
本文介绍了在对象数组的情况下如何删除Json模式中顺序的依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Json模式模板,其中包含对象数组.我需要使用该模板来验证Json输入.但是我希望这不应该依赖于数组中对象的顺序.

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.

在下面,我们在模板中有3个不同对象的数组,即abs,端点和调度.我想从这里删除订单的依存关系.我可以在Json输入模式中提供项目的蚂蚁顺序.它不应该依赖模板.我正在使用'ajv'节点js模板来验证带有模板数据的Json输入.任何帮助,将不胜感激.谢谢.

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"
        }
  ]
}`

推荐答案

items关键字采用单个模式,也可以采用一个或多个数组.

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

如果提供模式数组,则必须将第一个模式应用于数组中的第一项,然后将第二个模式应用于数组中的第二项,对于n个模式和项,依此类推.

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.

要更改模式,以使items的值是单个模式,请将模式数组包装在oneOf中.这将意味着数组中的每个项目都必须对oneOf数组值中的模式之一有效.

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.

这篇关于在对象数组的情况下如何删除Json模式中顺序的依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆