我有一个JSON模式(v7.0),我需要确保在最终的formData中存在所列出的属性。
对于大多数情况,这可以使用"additionalProperties": false
和"minProperties": X
来解决,其中X代表对象中的#properties(this solution的恭维)
但是,在某些情况下,属性的数量是可变的。
在以下示例中是这种情况,其中在“sync”中选择“手动更新”(枚举2)可以导致添加字段“manual_date”。当“sync”设置为“Manual update”时,需要“manual_date”属性 - 但不应该要求它。
我尝试通过依赖项下的“required”语句实现这一点,但我认为这是错误的,因为我在通过validator测试时得到了一些非常广泛的错误消息。
简而言之:我正在寻找在JSON模式中使用依赖于枚举的字段的正确方法。
我的架构:
{
"type": "object",
"properties": {
"rtc": {
"title": "REAL-TIME-CLOCK (RTC)",
"type": "object",
"properties": {
"sync": {
"title": "Time synchronization method",
"type": "integer",
"default": 1,
"anyOf": [
{
"type": "integer",
"title": "Retain current time",
"enum": [
1
]
},
{
"type": "integer",
"title": "Manual update",
"enum": [
2
]
},
{
"type": "integer",
"title": "Automatic update (requires WiFi)",
"enum": [
3
]
}
]
},
"timezone": {
"title": "Time zone (UTC−12 to UTC+14)",
"type": "number",
"default": 0,
"minimum": -12,
"maximum": 14
},
"adjustment": {
"title": "Adjustment (-129600 to 129600 seconds)",
"type": "number",
"default": 0,
"minimum": -129600,
"maximum": 129600
}
},
"dependencies": {
"sync": {
"oneOf": [
{
"properties": {
"sync": {
"enum": [
1
]
}
}
},
{
"properties": {
"sync": {
"enum": [
2
]
},
"manual_date": {
"title": "Time (UTC) ",
"type": "string",
"format": "date-time"
}
},
"required": [
"manual_date"
]
},
{
"properties": {
"sync": {
"enum": [
3
]
}
}
}
]
}
},
"additionalProperties": false,
"patternProperties": {
"manual_date": {}
},
"minProperties": 3
}
}
}
更新:我没有找到直接的解决方案。但是,可以通过使用additionalProperties来完成部分解决方案:false和minProperties:X组合使用。这有助于确保包含适量的字段 - 并且仅包括正确的字段。