我是否可以拥有在对象数组中添加字段的 JSON 架构依赖项?



我有一个包含对象数组的JSON模式。我希望这些对象中的一个字段依赖于数组外部的属性。

我创建了以下架构:

{
type: "object",
properties: {
showNotes: {
title: "Add notes field",
type: "boolean",
},
choices: {
type: "array",
title: "Choices",
items: {
type: "object",
properties: {
itemName: {
type: "string",
title: "Item"
},
isRequired: {
type: "boolean",
title: "Required?",
default: false
},
note: {}
}
},
default: [
{
content: "Thing 1",
correct: false
}
]
}
},
dependencies: {
showNotes: {
oneOf: [
{
properties: {
showNotes: {
enum: [
false
]
}
}
},
{
properties: {
showNotes: {
enum: [
true
]
},
note: {
title: "Note",
type: "string"
}
}
}
]
}
}
}

我希望新字段note会更新items/note的字段,但它没有,而是在底部生成了一个新字段note。这可以在这里看到:

https://codepen.io/samfentr/pen/ZEQpeyg

我认为该解决方案与添加$ref引用有关,但我无法解决。

我能够解决这个问题。

我需要将整个路径放在我的oneOf选项中notes

showNotes: {
oneOf: [
{
properties: {
showNotes: {
enum: [
false
]
}
}
},
{
properties: {
showNotes: {
enum: [
true
]
},
choices: {
items: {
properties: {
note: {
type: "string",
title: "Note"
}
}
}
}
}
}
]
}

https://codepen.io/samfentr/pen/BajQwYE

最新更新