darkrai
darkrai

Reputation: 123

JSON Schema with Nested Objects with different properties

The entire JSON file is rather large so I've only taken out the subsection I've had an issue with.

{
  "diagrams": {
    "5f759d15cd046720c28531dd": {
      "_id": "5f759d15cd046720c28531dd",
      "offsetX": 320,
      "offsetY": 42,
      "zoom": 80,
      "modified": 1604279356,
      "nodes": {
        "5f9f5c3ccd046720c28531e4": {
          "nodeID": "5f9f5c3ccd046720c28531e4",
          "type": "start",
          "coords": [
            360,
            120
          ],
          "data": {
            "name": "Start",
            "color": "standard",
            "ports": [
              {
                "type": "",
                "target": "5f9f5c3ccd046720c28531e6"
              }
            ],
            "steps": []
          }
        },
        "5f9f5c3ccd046720c28531e5": {
          "nodeID": "5f9f5c3ccd046720c28531e5",
          "type": "block",
          "coords": [
            760,
            120
          ],
          "data": {
            "name": "Help Message",
            "color": "standard",
            "steps": [
              "5f9f5c3ccd046720c28531e6",
              "5f9f5c3ccd046720c28531e7"
            ]
          }
        },
        "5f9f5c3ccd046720c28531e6": {
          "nodeID": "5f9f5c3ccd046720c28531e6",
          "type": "speak",
          "data": {
            "randomize": false,
            "dialogs": [
              {
                "voice": "Alexa",
                "content": "You said help. Do you want to continue?"
              }
            ],
            "ports": [
              {
                "type": "",
                "target": "5f9f5c3ccd046720c28531e7"
              }
            ]
          }
        },
        "5f9f5c3ccd046720c28531e7": {
          "nodeID": "5f9f5c3ccd046720c28531e7",
          "type": "interaction",
          "data": {
            "name": "Choice",
            "else": {
              "type": "path",
              "randomize": false,
              "reprompts": []
            },
            "choices": [
              {
                "intent": "",
                "mappings": []
              },
              {
                "intent": "",
                "mappings": []
              }
            ],
            "reprompt": null,
            "ports": [
              {
                "type": "else",
                "target": null
              },
              {
                "type": "",
                "target": null
              },
              {
                "type": "",
                "target": "5f9f5c3ccd046720c28531e9"
              }
            ]
          }
        },
        "5f9f5c3ccd046720c28531e8": {
          "nodeID": "5f9f5c3ccd046720c28531e8",
          "type": "block",
          "coords": [
            1170,
            260
          ],
          "data": {
            "name": "Exit",
            "color": "standard",
            "steps": [
              "5f9f5c3ccd046720c28531e9"
            ]
          }
        },
        "5f9f5c3ccd046720c28531e9": {
          "nodeID": "5f9f5c3ccd046720c28531e9",
          "type": "exit",
          "data": {
            "ports": []
          }
        }
      },
      "children": [],
      "creatorID": 42661,
      "variables": [],
      "name": "Help Flow",
      "versionID": "5f759d15cd046720c28531db"
    }
    }
}

The Current JSON Schema Definition I have is:

{
  "$schema":"http://json-schema.org/schema#",
  "type":"object",
  "properties":{
     "diagrams":{
        "type":"object"
     }
  },
  "required":[
     "diagrams",
  ]
}

The problem I am having is that within diagrams contains multiple objects with a random string as the name e.g "5f759d15cd046720c28531dd".

Then within that object there are properties such as (_id, offsetX) which I want to express as well as a nodes object, which again contains multiple objects with arbitrary names e.g ("5f9f5c3ccd046720c28531e4", "5f9f5c3ccd046720c28531e5", ...) which have a unique node definition where some nodes have different properties to other nodes (nodeID, type, data vs nodeID, type, data, coords).

My question is with all these arbitrary things such as random names as well as different properties per each node. How do I turn it into 1 JSON schema definition which covers all the cases of how a diagram/node can be made.

Upvotes: 0

Views: 4341

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24489

You can do this with additionalProperties or patternProperties.

additionalProperties applies to any property that isn't declared in properties or patternProperties.

{
  "type": "object",
  "additionalProperties": {
    "type": "object",
    "properties": {
      "_id": { ... },
      "offsetX": { ... },
      ...
    }
  }
}

Your property names appear to always be hex numbers. If you want to enforce that those property names are always hex numbers, you can use patternProperties. Any property that matches the regex must conform to that schema.

{
  "type": "object",
  "patternProperties": {
    "^[0-9a-f]{24}$": {
      "type": "object",
      "properties": {
        "_id": { ... },
        "offsetX": { ... },
        ...
      }
    }
  },
  "additionalProperties": false
}

Upvotes: 3

Related Questions