jsonschema - 在对象数组的情况下,如何删除 Json 模式中顺序的依赖关系

标签 jsonschema json-schema-validator ajv

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

下面我们在模板中有 3 个不同对象的数组,即ABS、端点和调度。我想从这里删除顺序的依赖性。我可以在 Json 输入模式中提供项目的 ant 顺序。它不应该依赖于模板。我正在使用“ajv”节点 js 模板来使用模板数据验证 Json 输入。任何帮助,将不胜感激。谢谢。

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 关键字可以采用单个架构,也可以采用一个或多个架构。

如果您提供架构数组,则第一个架构必须应用于数组中的第一个项目,然后第二个架构应用于数组中的第二个项目,依此类推,对于 n 个架构和项目。

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

关于jsonschema - 在对象数组的情况下,如何删除 Json 模式中顺序的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52713839/

相关文章:

json - 如何引用顶级数组 json 的模式

c# - newtonsoft json 模式反序列化 ValidationError

java - 使用maven repo分发JSON Schema文件

javascript - AJV 多级/嵌套 JSON 模式验证

node.js - 将 Sequelize 模型转换为 JSON Schema 以进行用户输入验证

xml - JSON 架构与 XML 架构

json - 分页 jsonschema/hyperschema 时保留查询参数

java - 如何从命令行运行 json-schema-validator

jsonschema - `true` 的 `schema` 值的定义行为在哪里?

javascript - 使用 AJV 验证针对 JSON 架构的 API 响应