删除使用 oneOf(v4 或 v5)的 JSON 模式中的重复项

Removing the duplication in a JSON schema that uses oneOf (v4 or v5)

我有一组 2 个属性,它们始终是可选的,但只有在另一个(始终需要)布尔值 属性 的值为真时才允许存在。

始终可选但并非始终允许的属性命名为:max_recurrencesrecurrence_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_dateend_datetriggerarguments .

JSON 架构草稿 04:

{
  "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"] }
        }
      }
    }
  ]
}

如果您使用 Ajv(我假设是这样,因为 v5 是其他任何地方都没有使用的概念),您可以使用为 draft-07 和有一些支持 - 它们在 ajv-keywords 中定义。 "anyOf" 可以替换为:

"if": { "properties": { "recurring": { "enum": [false] } } },
"then": { "prohibited": ["max_recurrences", "recurrence_arguments"] }

编辑:

实际上,不用任何自定义关键字,使用 "dependencies" 关键字可以更简单。而不是 "anyOf":

"dependencies": {
  "max_recurrences": { "$ref": "#recurring" },
  "recurrence_arguments": { "$ref": "#recurring" }
},
"definitions": {
  "recurring": {
    "id": "#recurring",
    "properties": {
      "recurring": { "enum": [true] }
    }
  }
}