【问题标题】:Can I have a JSON Schema dependency that adds a field inside an array of objects?我可以拥有一个在对象数组中添加字段的 JSON Schema 依赖项吗?
【发布时间】:2020-06-15 20:39:05
【问题描述】:

我有一个包含对象数组的 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 引用有关,但我无法解决。

【问题讨论】:

    标签: jsonschema react-jsonschema-forms


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 2019-07-10
      • 1970-01-01
      相关资源
      最近更新 更多