0

我有一个包含对象数组的 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会更新 at 的那个items/note,但它没有,而是note在底部生成了一个新字段。这可以在这里看到:

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

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

4

1 回答 1

1

我能够解决这个问题。

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

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

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

于 2020-06-17T16:03:11.553 回答