Jsonschema validate if the list contains one of the three strings

75 Views Asked by At

I have a json like this: {"mylist": ["a", "b", "c", "d", additional_element]}

additional_element could be one of the following four strings - "w", "x", "y", "z"; but not multiple

Examples:

{
    "mylist": ["a", "b", "c", "d", "x"] // <-- valid
}

{
    "mylist": ["a", "b", "c", "d", "x", "y"] // <-- not valid
} 

How to write a jsonschema for this?

Current attempt:

{
   "type":"object",
   "properties":{
      "mylist":{
         "type":"array",
         "items":{
            "type":"string",
            "enum":[
               "a",
               "b",
               "c",
               "d",
               "w",
               "x",
               "y",
               "z"
            ]
         },
         "minItems":1,
         "uniqueItems":true
      }
   },
   "required":[
      "mylist"
   ],
   "additionalProperties":false
}

Edit:

additional_element

  • could be placed anywhere in the list.
  • is optional

Additional examples:

    {
        "mylist": ["b", "a", "y"] // <-- valid
    } 

    {
        "mylist": ["z", "b", "a"] // <-- valid
    }

    {
        "mylist": ["c"] // <-- valid
    }

    {
        "mylist": ["y"] // <-- valid
    }
1

There are 1 best solutions below

1
Jeremy Fiel On

You didn't mention which draft version you are using so I'll give you two examples, they both behave similarly.

Draft-04 - 2019-09

Define the array with each index's expected value with items[] and constraint any additionalItems from being used

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "mylist": {
            "type": "array",
            "items": [
                {
                    "$ref": "#/definitions/startOfArrayItems"
                },
                {
                    "$ref": "#/definitions/startOfArrayItems"
                },
                {
                    "$ref": "#/definitions/startOfArrayItems"
                },
                {
                    "$ref": "#/definitions/startOfArrayItems"
                },
                {
                    "enum": [
                        "w",
                        "x",
                        "y",
                        "z"
                    ]
                }
            ],
            "minItems": 1,
            "additionalItems": false,
            "uniqueItems": true
        }
    },
    "required": [
        "mylist"
    ],
    "additionalProperties": false,
    "definitions": {
        "startOfArrayItems": {
            "enum": [
                "a",
                "b",
                "c",
                "d"
            ]
        }
    }
}

Draft 2020-12

A new keyword prefixItems was introduced and takes the same behavior and syntax as the older draft versions usage of items[]. You can disallow additional items in the array with items: false

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "mylist": {
            "type": "array",
            "prefixItems": [
                {
                    "$ref": "#/$defs/startOfArrayItems"
                },
                {
                    "$ref": "#/$defs/startOfArrayItems"
                },
                {
                    "$ref": "#/$defs/startOfArrayItems"
                },
                {
                    "$ref": "#/$defs/startOfArrayItems"
                },
                {
                    "enum": [
                        "w",
                        "x",
                        "y",
                        "z"
                    ]
                }
            ],
            "minItems": 1,
            "items": false,
            "uniqueItems": true
        }
    },
    "required": [
        "mylist"
    ],
    "additionalProperties": false,
    "$defs": {
        "startOfArrayItems": {
            "enum": [
                "a",
                "b",
                "c",
                "d"
            ]
        }
    }
}