简体   繁体   English

如何为对象的动态嵌套 map 创建 JSON 模式

[英]How to create JSON schema for dynamic nested map of objects

I am trying to create JSON schema for below JSON,我正在尝试为以下 JSON 创建 JSON 架构,

{
"messages":{
      "bookCreated":{
        "default":{
          "channels":{
            "sqs":{
              "enabled":true,
              "topic":"sample-topic",
              "shares":true
            }
          }
        }
      },
       "bookCreationInProgress":{
        "default":{
          "channels":{
            "sns":{
              "enabled":true,
              "topic":"sample-sns",
              "shares":true
            }
          }
        }
      },
      "bookCreationCompleted":{
        "default":{
          "channels":{
            "s3":{
              "enabled":true,
              "topic":"sample-s3-bucket",
              "shares":true
            }
          }
        }
      }
}
}

Inside message bookCreated , bookCreationInProgress , bookCreationCompleted similarly we have several dynamic properties.在消息bookCreatedbookCreationInProgressbookCreationCompleted中,我们同样有几个动态属性。 Inside each of these objects default and channel details are mandatory.在这些对象中的每一个中,默认和通道详细信息都是强制性的。 And each channel has a set of mandatory attributes.每个通道都有一组强制属性。

I browsed inte.net to create JSON schema for the above json but I couldn't get any reference of how to create json schema for nested map objects.我浏览了 inte.net 为上面的 json 创建了 JSON 模式,但是我无法获得关于如何为嵌套的 map 对象创建 json 模式的任何参考。

Since I couldn't able to construct the json schema for very first dynamic object I couldn't able to construct the schema further.由于我无法为第一个动态 object 构建 json 模式,因此我无法进一步构建该模式。

{
  "$schema": "app_messages",
  "type": "object",
  "additionalProperties": true,
  "anyOf": [
    {
      "required": ["messages"]
    }
  ],
  "properties": {
    "id": {
      "type": "string"
    }
  }
}

It would be really great if somebody would help me to share the pointers of how to handle map of dynamic properties in JSON schema.如果有人能帮助我分享如何处理 JSON 模式中动态属性的 map 的指针,那就太好了。 Any help would be really appreciable.任何帮助都会非常可观。

A good solution use "additionalProperties" as json-object (instead of boolean) combined to "$ref" and "$defs".一个好的解决方案是使用“additionalProperties”作为 json 对象(而不是布尔值)组合到“$ref”和“$defs”。 A talking example might be:一个会说话的例子可能是:

    {
       "$schema": "app_messages",
       "type": "object",
       "additionalProperties": {
          "anyOf": [
            { "$ref": "#/$defs/subobj1" },
            { "$ref": "#/$defs/subobj2" }
          ]
       },
       "properties": {
         "id": { "type": "string" }
       },
       "$defs": {
         "subobj1": {
            "type": "object",
            "properties": {
               "message": { "type": "string" }
               ...
            },
            "required": [ "message" ]
         },
         "subobj2": {
            "type": "object",
            "properties": {
               "message": { "type": "string" }
               ...
            },
            "required": [ "message" ]
         }
       }
     }

In this way main object can have "id" properties and all additional properties must match one of sub definition "subobj1" or "subobj2".通过这种方式,main object 可以具有“id”属性,并且所有附加属性必须匹配子定义“subobj1”或“subobj2”之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM