简体   繁体   中英

How to create JSON schema for dynamic nested map of objects

I am trying to create JSON schema for below 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. 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.

Since I couldn't able to construct the json schema for very first dynamic object I couldn't able to construct the schema further.

{
  "$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. Any help would be really appreciable.

A good solution use "additionalProperties" as json-object (instead of boolean) combined to "$ref" and "$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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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