JSON PatternProperties 中的模式严格类型

JSON Schema strict typing in PatternProperties

我有 JSON 类似于下面的文档

{
  "dell": {
    "memory": {

      "RAM": {
        "RamType": "DDR",
        "Size": 16
      },
      "Graphic": {
        "RamType": "GDDR",
        "Size": 4,
        "dummy": "dummy"
      }
    },
    "dummy": "dummy"
  }
}

我想要的是一个键值对列表,其中键是计算机的名称,值是计算机的属性。同样在这些值中,我可以有多个键值对,如上所示。

我已经用 Draft 6 编写了 JSON 架构,如下所示

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "properties": {
    "computers": {
      "patternProperties": {
        "additionalProperties": false,
        "^[a-z0-9-_]+$": {
          "properties": {
            "memory": {
              "patternProperties": {
                "^[a-z0-9-_]+$": {
                  "additionalProperties": false,
                  "properties": {
                    "RamType": {
                      "type": "string",
                      "RamSize": {
                        "type": "number"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

您可以尝试一下here

问题是我不想让用户输入 "dummy" 值,如上面给出的 JSON 所示。显然 "additionalProperties" : true 不起作用。我能做什么?

您的主要问题是您的架构描述了一个名为 "computers" 的顶级 属性,而您的数据没有。因此,none 您正在测试的数据完全受到您的模式的限制。另一个问题是您的正则表达式与大写字符不匹配。

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "patternProperties": {
    "^[a-zA-Z0-9-_]+$": {
      "additionalProperties": false,
      "properties": {
        "memory": {
          "patternProperties": {
            "^[a-zA-Z0-9-_]+$": {
              "additionalProperties": false,
              "properties": {
                "RamType": { "type": "string" },
                "Size": { "type": "number" }
              }
            }
          }
        }
      }
    }
  }
}