【问题标题】:JSON Schema - Setting Static NamesJSON Schema - 设置静态名称
【发布时间】:2021-05-27 01:53:05
【问题描述】:

我试图转换为模式的 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"?

谢谢! :)

【问题讨论】:

    标签: json validation schema jsonschema json-schema-validator


    【解决方案1】:

    您需要为每个 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"
      ]
    }
    

    【讨论】:

    • 或者在更新版本的 JSON Schema 中,也可以使用 const 指定单个枚举值。
    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 2015-11-09
    • 2020-06-18
    • 2013-07-29
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多