Search code examples
jsonvalidationcontainsjsonschemajson-schema-validator

Workaround for "minContains" & "maxContains" in JSON Schema validation draft-07?


The latest JSON schema validation release (2019-09) allows one to put conditions (sub-schema) using contains keyword and also how many times that condition can appear in the JSON schema using minContains & maxContains. This functionality is not available in draft-07. Is there any way to attain this without using these keywords? For e.g.

"answers": [
    {
      "id": 1,
      "text": "choice1",
      "isCorrect": true
    },
    {
      "id": 1,
      "text": "choice2",
      "isCorrect": false
    },
    {
      "id": 1,
      "text": "choice3",
      "isCorrect": false
    },
    {
      "id": 1,
      "text": "choice4",
      "isCorrect": false
    }
  ]
}

A condition with contains as below:

"contains":{
  "properties":{
    "isCorrect":{
      "enum":["true"]
     }
  }
}

This checks that isCorrect is true at least once. But if I would like the validation to pass only when the value of isCorrect is true at least twice, how can I achieve that? Any help is appreciated! Thanks.


Solution

  • No there is not, that is why we added the keywords for draft 2019-09. Sorry.