在使用oneOf(v4或v5)的JSON模式中删除重复项 [英] Removing the duplication in a JSON schema that uses oneOf (v4 or v5)

查看:83
本文介绍了在使用oneOf(v4或v5)的JSON模式中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组2个属性,这些属性始终是可选的,但是仅当另一个(总是需要)布尔属性的值为true时才应允许存在.

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.

始终为可选属性,但并非始终允许的属性被命名为:max_recurrencesrecurrence_arguments.他们所依赖的true值的布尔属性被命名为:recurring.

The properties which are always optional, but not always allowed are named: max_recurrences and recurrence_arguments. The boolean property whose value of true that they depend on is named: recurring.

我想出了下面的模式,我认为它可以工作,但是我正在复制oneOf数组的每个项目中的所有其他属性.我正在寻找一种避免重复的方法.

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

有人可以帮我吗?我想使用v4,但如果有帮助,我愿意使用v5.

Can anyone help me out? I'd like to use v4, but I'm open to using v5 if it helps.

为进一步说明,我希望只列出整个架构中的属性一次:start_dateend_datetriggerarguments.

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.

推荐答案

JSON模式草案04:

JSON Schema draft-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是在其他任何地方都没有使用过的概念),则可以使用为07草案建议的自定义关键字"if/then/else"和"prohibited"来简化上述操作,并使用一些支持-在 ajv-关键字中定义. "anyOf"可以替换为:

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. "anyOf" can be replaced with:

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

实际上,无需任何自定义关键字,使用"dependencies"关键字甚至可以更简单地完成此操作.而不是"anyOf":

Actually, it can be done even simpler with "dependencies" keyword without any custom keywords. Instead of "anyOf":

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

这篇关于在使用oneOf(v4或v5)的JSON模式中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆