簡體   English   中英

JSON 架構具有不同屬性的嵌套對象

[英]JSON Schema with Nested Objects with different properties

整個 JSON 文件相當大,所以我只取出了我遇到問題的小節。

{
  "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"
    }
    }
}

我擁有的當前 JSON 架構定義是:

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

我遇到的問題是圖表中包含多個對象,其名稱為隨機字符串,例如“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 eg ("5f9f5c3ccd046720c28531e4", "5f9f5c3ccd046720c28531e5", ...) which have一個獨特的節點定義,其中一些節點與其他節點具有不同的屬性(nodeID、type、data vs nodeID、type、data、coords)。

我的問題是所有這些任意的東西,例如隨機名稱以及每個節點的不同屬性。 如何將其轉換為 1 JSON 模式定義,它涵蓋了如何制作圖表/節點的所有情況。

您可以使用additionalPropertiespatternProperties來做到這一點。

additionalProperties適用於未在propertiespatternProperties中聲明的任何屬性。

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

您的屬性名稱似乎始終是十六進制數字。 如果您想強制這些屬性名稱始終是十六進制數字,您可以使用patternProperties 任何與正則表達式匹配的屬性都必須符合該模式。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM