我有一个JSON (v7.0),在这个模式中,我需要确保所列出的属性确实存在于最终的formData中。
在大多数情况下,这可以使用"additionalProperties": false和"minProperties": X来解决,其中X表示对象中的#属性(这个解决方案的礼貌)
但是,在某些情况下,属性的数量是可变的。。
下面的例子就是这种情况,其中选择“同步”中的“手动更新”(enum 2)可以导致添加"manual_date“字段。当“同步”设置为“手动更新”时,需要使用"manual_date“属性,但否则不应该要求它。
我尝试通过依赖项下的"required“语句实现这一点,但我认为这是错误的,因为我在通过验证器测试它时得到了一些非常广泛的错误消息。
因此,简而言之:我正在寻找在我的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
}
}
}发布于 2019-03-15 11:53:24
更新:我没有找到一个直接的解决办法。但是,可以结合使用additionalProperties: false和minProperties: X来实现部分解决方案。这有助于确保正确的字段数量--并且只包含正确的字段。
https://stackoverflow.com/questions/43084878
复制相似问题