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
}
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 usedDraft 2020-12
A new keyword
prefixItemswas introduced and takes the same behavior and syntax as the older draft versions usage ofitems[]. You can disallow additional items in the array withitems: false