0

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
    }
2
  • is your list always 5 indexes? Commented Feb 6, 2024 at 18:20
  • @JeremyFiel no not at all. Let me edit my example. Commented Feb 7, 2024 at 7:55

1 Answer 1

1

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"
            ]
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, upvoted. I have edited my question to reflect some more scenarios. Could you help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.