微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JSON 模式 - 设置静态名称

如何解决JSON 模式 - 设置静态名称

我试图转换为架构的示例 JSON 是:

{
          "nodeID": "5f9f5dbe0c3ab520c2b44bb0","type": "block","coords": [
            517.2814214277396,769.7697271579176
          ],"data": {
            "name": "Block","color": "standard","steps": [
              "5f9f5dbe0c3ab520c2b44bb1"
            ]
          }
        }

我已经把它转换成这个模式

{
  "$schema": "http://json-schema.org/draft-04/schema#","type": "object","properties": {
    "nodeID": {
      "type": "string"
    },"type": {
      "type": "string"
    },"data": {
      "type": "object","properties": {
        "name": {
          "type": "string"
        },"intent": {
          "type": "string"
        },"diagramID": {
          "type": "string"
        },"mappings": {
          "type": "array","items": {}
        },"next": {
          "type": "null"
        },"ports": {
          "type": "array","items": {}
        }
      },"required": [
        "name","intent","diagramID","mappings","next","ports"
      ]
    }
  },"required": [
    "nodeID","type","data"
  ]
}

在示例 JSON 中,您可以看到 type = "block"。 我如何在架构中确保在检查 JSON 时确保检查类型键是 ==“block”?

谢谢! :)

解决方法

您需要为每个 https://json-schema.org/understanding-json-schema/reference/generic.html#enumerated-values

使用一个枚举
{
  "$schema": "http://json-schema.org/draft-04/schema#","type": "object","properties": {
    "nodeID": {
      "type": "string"
    },"type": {
      "type": "string","enum": ["block"]
    },"data": {
      "type": "object","properties": {
        "name": {
          "type": "string"
        },"intent": {
          "type": "string"
        },"diagramID": {
          "type": "string"
        },"mappings": {
          "type": "array","items": {}
        },"next": {
          "type": "null"
        },"ports": {
          "type": "array","items": {}
        }
      },"required": [
        "name","intent","diagramID","mappings","next","ports"
      ]
    }
  },"required": [
    "nodeID","type","data"
  ]
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。