简体   繁体   English

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

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

I have a set of 2 properties that are always optional, but should only be allowed to be present if the value of another (always required) boolean property is true.我有一组 2 个始终可选的属性,但只有在另一个(始终需要)布尔属性的值为 true 时才允许出现。

The properties which are always optional, but not always allowed are named: max_recurrences and recurrence_arguments .这始终是可选的,但并不总是允许的属性被命名为: max_recurrencesrecurrence_arguments The boolean property whose value of true that they depend on is named: recurring .它们所依赖的值为true的布尔属性被命名为: recurring

I've come up with the schema below, which I think works, but I'm duplicating all of the other properties in each item of the oneOf array.我想出了下面的模式,我认为它有效,但我正在复制oneOf数组的每个项目中的所有其他属性。 I'm looking for a way to avoid this duplication.我正在寻找一种方法来避免这种重复。

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

Can anyone help me out?谁能帮我吗? I'd like to use v4, but I'm open to using v5 if it helps.我想使用 v4,但如果有帮助,我愿意使用 v5。

To clarify further, I'm hoping to only have to list the properties: start_date , end_date , trigger and arguments one time in the entire schema.为了进一步澄清,我希望只需要在整个架构中列出属性: start_dateend_datetriggerarguments一次。

JSON Schema draft-04: 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"] }
        }
      }
    }
  ]
}

If you use Ajv (I assume so because v5 is the concept not used anywhere else) you can simplify the above using custom keywords "if/then/else" and "prohibited" that are proposed for draft-07 and have some support - they are defined in ajv-keywords .如果您使用 Ajv(我认为是因为 v5 是其他任何地方都没有使用的概念),您可以使用为 Draft-07 提出的自定义关键字“if/then/else”和“prohibited”来简化上述内容,并获得一些支持 - 他们在ajv-keywords中定义。 "anyOf" can be replaced with: “anyOf”可以替换为:

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

EDIT:编辑:

Actually, it can be done even simpler with "dependencies" keyword without any custom keywords.实际上,使用“dependencies”关键字可以更简单地完成,而无需任何自定义关键字。 Instead of "anyOf":而不是“anyOf”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM