使用json模式验证数组json包含几个无序对象 [英] validate array json contains several unordered objects using json schema

查看:176
本文介绍了使用json模式验证数组json包含几个无序对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用json模式草稿7来验证数组包含几个无序对象.例如,数组应包含学生A,B,无论其顺序如何.

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

当前尝试

json模式contains关键字不支持列表

Current Attempt

json schema contains keyword doesn't support a list

json模式Tuple validation关键字必须排序

json schema Tuple validation keyword must be ordered

推荐答案

您需要allOf应用程序关键字.您需要定义多个contains子句.

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

allOf允许您定义必须全部通过的多个模式.

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"
          }
        }
      }
    }
  ]
}

现场演示这里.

这篇关于使用json模式验证数组json包含几个无序对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆