validate array json contains several unordered objects using json schema

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-05 07:15:09

问题


Problem

I want to use json schema draft 7 to validate that an array contains several unordered objects. For example, the array should contains student A, B, regardless of their orders.

[{"name": "A"}, {"name": "B"}] //valid
[{"name": "B"}, {"name": "A"}] //valid
[{"name": "A"}, {"name": "C"}, {"name": "B"}] //extra students also valid
[] or [{"name": "A"}] or [{"name": "B"}] //invalid

Current Attempt

json schema contains keyword doesn't support a list

json schema Tuple validation keyword must be ordered


回答1:


You want the allOf applicator keyword. You need to define multiple contains clauses.

allOf allows you to define multiple schemas which must all pass.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "allOf": [
    {
      "contains": {
        "required": ["name"],
        "properties": {
          "name": {
            "const": "A"
          }
        }
      }
    },
    {
      "contains": {
        "required": ["name"],
        "properties": {
          "name": {
            "const": "B"
          }
        }
      }
    }
  ]
}

Live demo here.



来源:https://stackoverflow.com/questions/59176042/validate-array-json-contains-several-unordered-objects-using-json-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!