0

I'm trying to use JSON Schema validation to ensure that my array favoriteVegetables includes at least one value from my vegetables enum (carrot, lettuce, cucumber).

The array can have any and all of these values, but it must have at least one value.

Here's my schema:

{
  "$id": "https://www.example.com/vegetables.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Vegetables",
  "required": [ "favoriteVegetables" ],
  "type": "object",
  "properties": {
    "favoriteVegetables": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": ["carrot", "lettuce", "cucumber"]
      }
    },
  },
  "additionalProperties": false
}

The problem is that this validates for an object where favoriteVegetables is []. How do I add the requirement "must have at least one value" (cannot be a null array)?

Patrick Kenny
  • 4,515
  • 7
  • 47
  • 76
  • Duplicate of [How to define the min size of array in the JSON Schema](https://stackoverflow.com/questions/16583485/how-to-define-the-min-size-of-array-in-the-json-schema) – Helen Aug 25 '22 at 16:02

1 Answers1

2

You can use the "minItems": 1 keyword.

Reference: https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minitems

erosb
  • 2,943
  • 15
  • 22