关于这个主题有几个问题,但似乎都没有解决这个特定问题,JSON Schema 上的文档也没有,所以可能无法完成。
问题是我有一个数组,可以有 4 个字符串中的任何一个作为值,很容易用这个模式实现:
...
"attributes": {
"type": "array",
"items": {
"type": "string",
"enum": [
"controls",
"autoplay",
"muted",
"loop"
]
},
"additionalItems": false
}
...
所以数组中的值只能是这四个之一。然而,“控件”必须始终是数组的一部分,而其他三个是可选的。如果它是一个对象数组,我们可以将其设为必需,但我不确定如何检查具有特定值的数组。
感谢您的帮助!
最佳答案
您可以使用 contains
关键字:
"attributes": {
"type": "array",
"items": {
"type": "string",
"enum": [
"controls",
"autoplay",
"muted",
"loop"
]
},
"contains": {
"const": "controls"
},
"additionalItems": false
}
来自规范:
6.4.6. contains
The value of this keyword MUST be a valid JSON Schema.
An array instance is valid against "contains" if at least one of its elements is valid against the given schema.
关于arrays - JSON Schema Array 必须包含特定的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54943748/