0

在我见过的大多数 json 模式示例中,关键字“items”似乎与“array”相关联。但是我尝试使用这个有用的工具将它与对象一起使用:http: //www.jsonschemavalidator.net/(JSON Schema Draft 4)并且它可以工作。我找不到任何文件表明这是合法的,尽管我觉得它是正确的。

"value": { 
      "type": "object",
      "items": ...
}

这真的合法吗?

4

1 回答 1

1

这是您正在寻找的文档。

一些验证关键字仅适用于一种或多种原始类型。当实例的原始类型无法通过给定关键字验证时,此关键字和实例的验证应该成功。

为了说明这个概念,以这个模式为例。

{
  "items": { "type": "string" },
  "maxLength": 2,
  "required": ["foo"]
}

["foo"]验证

  • items-> 通过
  • maxLength-> 忽略
  • required-> 忽略

{ "foo": "bar" }验证

  • items-> 忽略
  • maxLength-> 忽略
  • required-> 通过

"foo"不验证

  • items-> 忽略
  • maxLength-> 失败
  • required-> 忽略

3验证

  • items-> 忽略
  • maxLength-> 忽略
  • required-> 忽略

尽管可以以这种方式编写模式,但建议不要在单个模式中混合类型关键字。anyOf相反,您可以获得更具可读性的模式。

{
  "anyOf": [
    {
      "type": "string",
      "maxLength": 2
    },
    {
      "type": "array",
      "items": { "type": "string" }
    },
    {
      "type": "object",
      "required": "foo"
    }
  ]
}
于 2016-06-06T05:00:54.080 回答