我有一组2个属性,它们总是可选的,但只有在另一个(总是需要的)布尔属性的值为true时才允许存在。
始终可选但不总是允许的属性名为:max_recurrences
和recurrence_arguments
。它们所依赖的值为true
的布尔属性名为:recurring
。
我已经提出了下面的架构,我认为这样可行,但我在oneOf
阵列的每个项目中复制了所有其他属性。我正在寻找避免这种重复的方法。
{
"id": "plan_schedule",
"type": "object",
"oneOf": [
{
"properties": {
"start_date": {
"type": "string",
"format": "date-time"
},
"end_date": {
"type": "string",
"format": "date-time"
},
"trigger": {
"$ref": "re_non_empty_string"
},
"arguments": {
"type": "object",
"minProperties": 1
},
"recurring": {
"type": "boolean",
"enum": [true],
},
"max_recurrences": {
"type": "integer",
"minimum": 1
},
"recurrence_arguments": {
"type": "object",
"minProperties": 1
}
}
},
{
"properties": {
"start_date": {
"type": "string",
"format": "date-time"
},
"end_date": {
"type": "string",
"format": "date-time"
},
"trigger": {
"$ref": "re_non_empty_string"
},
"arguments": {
"type": "object",
"minProperties": 1
},
"recurring": {
"type": "boolean",
"enum": [false],
},
}
}
],
"additionalProperties": false,
"required": ["start_date", "trigger", "recurring"]
}
任何人都可以帮助我吗?我想使用v4,但如果它有帮助,我可以使用v5。
为了进一步澄清,我希望只需要在整个架构中列出属性:start_date
,end_date
,trigger
和arguments
答案 0 :(得分:1)
JSON Schema draft-04:
import QtQuick 2.7
import QtQuick.Window 2.2
Window {
width: 600
height: 600
visible: true
Component {
id: element
Rectangle {
width: Math.round(Math.random() * 100) + 50
height: Math.round(Math.random() * 100) + 50
color: Qt.rgba(Math.random(),Math.random(),Math.random(),1)
}
}
Flow {
id: flow
spacing: 2
anchors.fill: parent
add: Transition {
NumberAnimation { properties: "x,y"; easing.type: Easing.OutBack }
}
move: add
}
Timer {
id: timer
property bool is_add: true
interval: 300
repeat: true
running: true
onTriggered: {
if(timer.is_add) {
element.createObject(flow);
if(flow.children.length > 20) {
timer.is_add = false;
}
} else {
var item = flow.children[0];
item.destroy();
if(flow.children.length <= 1) {
timer.is_add = true;
}
}
}
}
}
如果您使用Ajv(我假设是因为v5是其他地方没有使用的概念),您可以使用为草案-07提议的自定义关键字“if / then / else”和“prohibited”来简化上述操作支持 - 它们在ajv-keywords中定义。 “anyOf”可以替换为:
{
"type": "object",
"properties": {
"recurring": {
"type": "boolean"
}
// all other properties
}
"additionalProperties": false,
"required": ["start_date", "trigger", "recurring"]
"anyOf": [
{
"properties": { "recurring": { "enum": [true] } }
},
{
"properties": { "recurring": { "enum": [false] } },
"not": {
"anyOf": [
{ "required": ["max_recurrences"] },
{ "required": ["recurrence_arguments"] }
}
}
}
]
}
编辑:
实际上,使用“dependencies”关键字可以更简单地完成它而不需要任何自定义关键字。而不是“anyOf”:
"if": { "properties": { "recurring": { "enum": [false] } } },
"then": { "prohibited": ["max_recurrences", "recurrence_arguments"] }