Correct way to define array of enums in JSON schema

senasi picture senasi · Jun 18, 2015 · Viewed 63.8k times · Source

I want to describe with JSON schema array, which should consist of zero or more predefined values. To make it simple, let's have these possible values: one, two and three.

Correct arrays (should pass validation):

[]
["one", "one"]
["one", "three"]

Incorrect:

["four"]

Now, I know the "enum" property should be used, but I can't find relevant information where to put it.

Option A (under "items"):

{
    "type": "array",
    "items": {
        "type": "string",
        "enum": ["one", "two", "three"]
    }
}

Option B:

{
    "type": "array",
    "items": {
        "type": "string"
    },
    "enum": ["one", "two", "three"]
}

Answer

jruizaranguren picture jruizaranguren · Jun 19, 2015

Option A is correct and satisfy your requirements.

{
    "type": "array",
    "items": {
        "type": "string",
        "enum": ["one", "two", "three"]
    }
}