繁体   English   中英

使用JSON模式验证数字/布尔值的嵌套列表

[英]Validating nested list of numbers/booleans with JSON schema

我不确定JSON模式是否可行,但是我有类似的数据:

[1, 1, [0, 0, [true]], true]

如何验证[0,0,1],以便至少一项是1 / true?

到目前为止,我已经设法创建了架构:

 { "type": "array", "items": { "$ref": "#/definitions/_items" }, "definitions": { "_items": { "anyOf": [ { "enum": [ 0, 1 ], "type": "integer" }, { "enum": [ false, true ], "type": "boolean" }, { "type": "array", "items": { "anyOf": [ { "$ref": "#/definitions/_items" } ] } } ] } } } 

显然,它确实验证了所有可接受的值,但是,如果存在值1 / true的全部,部分,一个或不存在,则不考虑在内。 我误会了,anyOf,allOf和oneOf是为此保留的...

您需要一个contains关键字。 计划将其添加到下一版本的JSON Schema规范中。 在实现之前,您可以不contains来完成此操作,但是逻辑有点复杂。 到目前为止,我还清理了一些不必要的内容。

{
  "type": "array",
  "items": { "$ref": "#/definitions/_items" },
  "allOf": [{ "$ref": "#/definitions/contains-1-or-true" }],
  "definitions": {
    "_items": {
      "anyOf": [
        { "enum": [0, 1] },
        { "type": "boolean" },
        { "$ref": "#" }
      ]
    },
    "contains-1-or-true": {
      "not": {
        "type": "array",
        "items": {
          "not": { "enum": [1, true] }
        }
      }
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM