title: Requiring certain elements to exist in an array using AJV
body: <p>I would like to validate a JSON object using a schema to ensure that all the elements specified in the schema exist in the array. For example, my people array MUST contain these people:</p>
  <pre><code>{
    &quot;people&quot;:[
      {
        &quot;name&quot;:&quot;Bob&quot;,
        &quot;age&quot;:&quot;26&quot;
      },
      {
        &quot;name&quot;:&quot;Jim&quot;,
        &quot;age&quot;:&quot;35&quot;
      },
      {
        &quot;name&quot;:&quot;John&quot;,
        &quot;age&quot;:&quot;27&quot;
      }
    ]
  }
  </code></pre>
  <p>Any additional people are ignored but these have to exist. I've tried to use allOf in the items specifier but it fails since allOf specifies that each element should match each of the provided schemas.</p>
  <pre><code>{
    &quot;people&quot;:{
      &quot;type&quot;:&quot;array&quot;,
      &quot;items&quot;:{
        &quot;allOf&quot;:[
          {
            &quot;type&quot;:&quot;object&quot;,
            &quot;properties&quot;:{
              &quot;name&quot;:{
                &quot;type&quot;:&quot;string&quot;,
                &quot;const&quot;:&quot;Bob&quot;
              },
              &quot;age&quot;:{
                &quot;type&quot;:&quot;string&quot;,
                &quot;const&quot;:&quot;26&quot;
              }
            },
            &quot;required&quot;:[
              &quot;name&quot;,
              &quot;age&quot;
            ]
          },
          {
            &quot;type&quot;:&quot;object&quot;,
            &quot;properties&quot;:{
              &quot;name&quot;:{
                &quot;type&quot;:&quot;string&quot;,
                &quot;const&quot;:&quot;Jim&quot;
              },
              &quot;age&quot;:{
                &quot;type&quot;:&quot;string&quot;,
                &quot;const&quot;:&quot;35&quot;
              }
            },
            &quot;required&quot;:[
              &quot;name&quot;,
              &quot;age&quot;
            ]
          },
          {
            &quot;type&quot;:&quot;object&quot;,
            &quot;properties&quot;:{
              &quot;name&quot;:{
                &quot;type&quot;:&quot;string&quot;,
                &quot;const&quot;:&quot;John&quot;
              },
              &quot;age&quot;:{
                &quot;type&quot;:&quot;string&quot;,
                &quot;const&quot;:&quot;27&quot;
              }
            },
            &quot;required&quot;:[
              &quot;name&quot;,
              &quot;age&quot;
            ]
          }
        ]
      }
    }
  }
  </code></pre>
  <p>With this information, we can conclude that the valid people array would be:</p>
  <pre><code>  [
      {
        &quot;name&quot;:&quot;Bob&quot;,
        &quot;age&quot;:&quot;26&quot;
      },
      {
        &quot;name&quot;:&quot;Jim&quot;,
        &quot;age&quot;:&quot;35&quot;
      },
      {
        &quot;name&quot;:&quot;John&quot;,
        &quot;age&quot;:&quot;27&quot;
      },
      {
        &quot;name&quot;:&quot;Tim&quot;,
        &quot;age&quot;:&quot;47&quot;
      }
    ]
  </code></pre>
  <p>and an invalid array would be:</p>
  <pre><code>  [
      {
        &quot;name&quot;:&quot;Bob&quot;,
        &quot;age&quot;:&quot;26&quot;
      },
      {
        &quot;name&quot;:&quot;Jim&quot;,
        &quot;age&quot;:&quot;35&quot;
      },
      {
        &quot;name&quot;:&quot;Tim&quot;,
        &quot;age&quot;:&quot;47&quot;
      }
    ]
  </code></pre>
  <p>since it's missing John from the list.</p>
  <p>Here is the code for the AJV file.</p>
  <pre><code>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);
  </code></pre>
  <p>What property would I use to specify which elements are required in an array as stated above?</p>
  <p>Edit: Based on the comment, I tried to use contains and haven't had any luck.</p>
  <pre><code>{
    &quot;$id&quot;: &quot;root&quot;,
    &quot;$schema&quot;: &quot;http://json-schema.org/draft-07/schema#&quot;,
    &quot;type&quot;: &quot;object&quot;,
    &quot;properties&quot;: {
      &quot;people&quot;: {
        &quot;type&quot;: &quot;array&quot;,
        &quot;items&quot;: {
          &quot;type&quot;: &quot;object&quot;,
          &quot;allOf&quot;: [
            {
              &quot;contains&quot;: {
                &quot;properties&quot;: {
                  &quot;name&quot;: {
                    &quot;enum&quot;: [&quot;Bob&quot;]
                  },
                  &quot;age&quot;: {
                    &quot;enum&quot;: [&quot;26&quot;]
                  }
                }
              }
            },
            {
              &quot;contains&quot;: {
                &quot;properties&quot;: {
                  &quot;name&quot;: {
                    &quot;enum&quot;: [&quot;John&quot;]
                  },
                  &quot;age&quot;: {
                    &quot;enum&quot;: [&quot;27&quot;]
                  }
                }
              }
            },
            {
              &quot;contains&quot;: {
                &quot;properties&quot;: {
                  &quot;name&quot;: {
                    &quot;enum&quot;: [&quot;Jim&quot;]
                  },
                  &quot;age&quot;: {
                    &quot;enum&quot;: [&quot;36&quot;]
                  }
                }
              }
            }
          ]
        }
      }
    },
    &quot;required&quot;: [&quot;people&quot;]
  }
  </code></pre>