JSON 架构数组必须包含特定字符串
JSON Schema Array must contain a specific string
关于这个主题有几个问题,但其中 none 似乎解决了这个特定问题,JSON 模式的文档也没有解决,所以可能无法完成。
问题是我有一个数组,可以将 4 个字符串中的任何一个作为值,使用此架构很容易实现:
...
"attributes": {
"type": "array",
"items": {
"type": "string",
"enum": [
"controls",
"autoplay",
"muted",
"loop"
]
},
"additionalItems": false
}
...
所以数组中的值只能是这四个中的一个。尽管如此,"controls" 必须始终是数组的一部分,而其他三个是可选的。如果它是一个对象数组,我们可以将其设为必需,但我不确定如何检查具有特定值的数组。
感谢您的帮助!
您可以使用 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.
关于这个主题有几个问题,但其中 none 似乎解决了这个特定问题,JSON 模式的文档也没有解决,所以可能无法完成。
问题是我有一个数组,可以将 4 个字符串中的任何一个作为值,使用此架构很容易实现:
...
"attributes": {
"type": "array",
"items": {
"type": "string",
"enum": [
"controls",
"autoplay",
"muted",
"loop"
]
},
"additionalItems": false
}
...
所以数组中的值只能是这四个中的一个。尽管如此,"controls" 必须始终是数组的一部分,而其他三个是可选的。如果它是一个对象数组,我们可以将其设为必需,但我不确定如何检查具有特定值的数组。
感谢您的帮助!
您可以使用 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.