title: How to define JSON Schema that requires an array of objects to contain at least 2 properties of a certain value?
body: <p>I have a dataset that looks like this:</p>
  
  <h3>Valid Example Data</h3>
  
  <pre><code>{
    type: "step",
    label: "Step 1",
    fields: [
      // An optional field
      {
        type: "email",
        label: "Your Email"
      },
      // A field that is required and can be either amount|preset
      {
        type: "amount",
        label: "Amount"
      },
      // A field that is required and can be either credit_card|ach
      {
        type: "credit_card",
        label: "Credit Card"
      }
    ]
  }
  </code></pre>
  
  <p>The <code>fields</code> array can contain many objects of various types. The above example would be valid.</p>
  
  <h3>Invalid Example Data</h3>
  
  <pre><code>{
    type: "step",
    label: "Step 1",
    fields: [
      {
        type: "email",
        label: "Your Email"
      },
      {
        type: "credit_card",
        label: "Credit Card"
      }
    ]
  }
  </code></pre>
  
  <p>This should error as it does not include an object of type <code>amount</code> or <code>presets</code></p>
  
  <h3>Validation Rules</h3>
  
  <p>In order to be valid, <code>fields</code> needs to contain 2 objects.</p>
  
  <ul>
  <li><p>1 of them must be of either <code>{ type: "amount" }</code> or <code>{ type: "presets" }</code></p></li>
  <li><p>1 of them must be of either <code>{ type: "credit_card" }</code> or <code>{ type: "ach" }</code></p></li>
  <li><p>Any combination of the 2 would make <code>fields</code> be valid.</p></li>
  </ul>
  
  <h2>JSON Schema</h2>
  
  <p>Here is my (failing) JSON Schema:</p>
  
  <pre><code>{
    "title": "step",
    "type": "object",
    "properties": {
      "type": {
        "title": "type",
        "type": "string"
      },
      "label": {
        "title": "label",
        "type": "string"
      },
      "fields": {
        "title": "fields",
        "description": "Array of fields",
        "type": "array",
        "additionalItems": true,
        "minItems": 1,
        "items": {
          "type": "object",
          "anyOf": [
            { "properties": { "type": { "enum": ["amount", "preset"] } } },
            { "properties": { "type": { "enum": ["credit_card", "ach"] } } }
          ],
          "properties": {
            "type": {
              "type": "string",
            }
          }
        },
      }
    },
    "required": ["type", "label", "fields"]
  }
  </code></pre>
  
  <p><a href="http://json-schema.org/latest/json-schema-validation.html" rel="nofollow noreferrer">Here is the JSON Schema Validation Reference</a></p>
  
  <p>I think between <code>contains</code>, <code>anyOf</code>, <code>allOf</code>, <code>oneOf</code>, and <code>enum</code> I should be able to do this?</p>