“解决方案”;minContains"&&引用;maxContains";在JSON模式验证草稿-07中?

“解决方案”;minContains"&&引用;maxContains";在JSON模式验证草稿-07中?,json,validation,contains,jsonschema,json-schema-validator,Json,Validation,Contains,Jsonschema,Json Schema Validator,最新的JSON模式验证发行版(2019-09)允许用户使用contains关键字放置条件(子模式),以及使用minContains和maxContains将该条件显示在JSON模式中的次数。此功能在草稿-07中不可用。有没有办法不用这些关键词就能做到这一点?例如 "answers": [ { "id": 1, "text": "choice1", "isCorre

最新的JSON模式验证发行版(2019-09)允许用户使用contains关键字放置条件(子模式),以及使用minContainsmaxContains将该条件显示在JSON模式中的次数。此功能在草稿-07中不可用。有没有办法不用这些关键词就能做到这一点?例如

"answers": [
    {
      "id": 1,
      "text": "choice1",
      "isCorrect": true
    },
    {
      "id": 1,
      "text": "choice2",
      "isCorrect": false
    },
    {
      "id": 1,
      "text": "choice3",
      "isCorrect": false
    },
    {
      "id": 1,
      "text": "choice4",
      "isCorrect": false
    }
  ]
}
条件包含以下内容:

"contains":{
  "properties":{
    "isCorrect":{
      "enum":["true"]
     }
  }
}
这将至少检查一次
isCorrect
是否
true
。但是,如果我希望验证只在
isCorrect
的值至少两次为
true
时通过,我如何实现这一点?
感谢您的帮助!谢谢。

没有,这就是为什么我们在草稿
2019-09
中添加了关键字。抱歉。

在draft-07中没有真正的方法来模拟
minContains
/
maxContains
。下面是一些你能得到的最接近的例子

“minContains”:2
,如果您有固定数量的元素。这并不漂亮,但您可以列举这两个项在数组中出现的所有可能方式

{
  "type": "array",
  "maxItems": 4,
  "minItems": 4,
  "anyOf": [
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    }
  ],
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    { "$ref": "#/definitions/isCorrect-true" },
    { "$ref": "#/definitions/isCorrect-true" }
  ],
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    true,
    true
  ],
  "additionalItems": { "not": { "$ref": "#/definitions/isCorrect-true" } },
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    { "$ref": "#/definitions/isCorrect-true" },
    { "$ref": "#/definitions/isCorrect-true" }
  ],
  "additionalItems": { "not": { "$ref": "#/definitions/isCorrect-true" } },
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
类似于
“minContains”:2
,除了所有
true
项需要在数组中位于第一位之外

{
  "type": "array",
  "maxItems": 4,
  "minItems": 4,
  "anyOf": [
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    }
  ],
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    { "$ref": "#/definitions/isCorrect-true" },
    { "$ref": "#/definitions/isCorrect-true" }
  ],
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    true,
    true
  ],
  "additionalItems": { "not": { "$ref": "#/definitions/isCorrect-true" } },
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    { "$ref": "#/definitions/isCorrect-true" },
    { "$ref": "#/definitions/isCorrect-true" }
  ],
  "additionalItems": { "not": { "$ref": "#/definitions/isCorrect-true" } },
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
类似于
“maxContains”:2
,除了所有
true
项需要在数组中位于第一位之外

{
  "type": "array",
  "maxItems": 4,
  "minItems": 4,
  "anyOf": [
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    }
  ],
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    { "$ref": "#/definitions/isCorrect-true" },
    { "$ref": "#/definitions/isCorrect-true" }
  ],
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    true,
    true
  ],
  "additionalItems": { "not": { "$ref": "#/definitions/isCorrect-true" } },
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    { "$ref": "#/definitions/isCorrect-true" },
    { "$ref": "#/definitions/isCorrect-true" }
  ],
  "additionalItems": { "not": { "$ref": "#/definitions/isCorrect-true" } },
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
“minContains”:2类似,“maxContains”:2
,除了所有
true
项需要在数组中位于第一位之外

{
  "type": "array",
  "maxItems": 4,
  "minItems": 4,
  "anyOf": [
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        true,
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    },
    {
      "items": [
        true,
        true,
        { "$ref": "#/definitions/isCorrect-true" },
        { "$ref": "#/definitions/isCorrect-true" }
      ]
    }
  ],
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    { "$ref": "#/definitions/isCorrect-true" },
    { "$ref": "#/definitions/isCorrect-true" }
  ],
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    true,
    true
  ],
  "additionalItems": { "not": { "$ref": "#/definitions/isCorrect-true" } },
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}
{
  "type": "array",
  "items": [
    { "$ref": "#/definitions/isCorrect-true" },
    { "$ref": "#/definitions/isCorrect-true" }
  ],
  "additionalItems": { "not": { "$ref": "#/definitions/isCorrect-true" } },
  "definitions": {
    "isCorrect-true": {
      "type": "object",
      "properties": {
        "isCorrect": { "const": true }
      }
    }
  }
}