使用AJV要求数组中存在某些元素

问题描述

我想使用一种模式来验证JSON对象,以确保该模式中指定的所有元素都存在于数组中。例如,我的人员数组必须包含以下人员:

{
  "people":[
    {
      "name":"Bob","age":"26"
    },{
      "name":"Jim","age":"35"
    },{
      "name":"John","age":"27"
    }
  ]
}

其他任何人都将被忽略,但这些人必须存在。我尝试在项目说明符中使用allOf,但是由于allOf指定每个元素应与每个提供的模式匹配,因此失败。

{
  "people":{
    "type":"array","items":{
      "allOf":[
        {
          "type":"object","properties":{
            "name":{
              "type":"string","const":"Bob"
            },"age":{
              "type":"string","const":"26"
            }
          },"required":[
            "name","age"
          ]
        },{
          "type":"object","const":"Jim"
            },"const":"35"
            }
          },"const":"John"
            },"const":"27"
            }
          },"age"
          ]
        }
      ]
    }
  }
}

有了这些信息,我们可以得出结论,有效的人员数组将是:

  [
    {
      "name":"Bob","age":"27"
    },{
      "name":"Tim","age":"47"
    }
  ]

一个无效的数组将是:

  [
    {
      "name":"Bob","age":"47"
    }
  ]

因为它从列表中丢失了约翰。

这是AJV文件代码

const Ajv = require('ajv');
const jsonInput = require('./people.json');
const schema = require('./peopleSchema.json');

const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(jsonInput);

console.log(valid);

if (!valid) console.log(validate.errors);

如上所述,我将使用什么属性来指定数组中需要哪些元素?

编辑:基于评论,我尝试使用contains并没有任何运气。

{
  "$id": "root","$schema": "http://json-schema.org/draft-07/schema#","type": "object","properties": {
    "people": {
      "type": "array","items": {
        "type": "object","allOf": [
          {
            "contains": {
              "properties": {
                "name": {
                  "enum": ["Bob"]
                },"age": {
                  "enum": ["26"]
                }
              }
            }
          },{
            "contains": {
              "properties": {
                "name": {
                  "enum": ["John"]
                },"age": {
                  "enum": ["27"]
                }
              }
            }
          },{
            "contains": {
              "properties": {
                "name": {
                  "enum": ["Jim"]
                },"age": {
                  "enum": ["36"]
                }
              }
            }
          }
        ]
      }
    }
  },"required": ["people"]
}

解决方法

通过使用以下内容作为模式,我可以满足我的要求。

{
  "properties": {
    "people": {
      "items": {
        "properties": {
            "name": { "type": "string" },"age": { "type": "string" }
        }
      },"allOf": [
        {
          "contains": {
            "const": {
                "name": "Bob","color": "26"
            }
          }
        },{
          "contains": {
            "const": {
                "name": "Jim","color": "35"
            }
          }
        },{
          "contains": {
            "const": {
                "name": "John","color": "27"
            }
          }
        }
        ]
    }
  },"required": [
    "people"
  ],"additionalProperties": false
}