json - 删除使用 oneOf(v4 或 v5)的 JSON 架构中的重复项

标签 json validation schema jsonschema

我有一组 2 个属性,它们始终是可选的,但仅当另一个(始终必需) bool 属性的值为 true 时才允许出现。

始终可选但并不总是允许的属性被命名为:max_recurrencesrecurrence_arguments。它们所依赖的值为 true 的 bool 属性名为: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 是其他任何地方都没有使用的概念),您可以使用为草案 07 提议的自定义关键字“if/then/else”和“prohibited”来简化上述内容,并且有一些支持 - 它们在 ajv-keywords 中定义。 “anyOf”可以替换为:

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

编辑:

实际上,使用“依赖项”关键字可以更简单地完成此操作,而无需任何自定义关键字。而不是“anyOf”:

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

关于json - 删除使用 oneOf(v4 或 v5)的 JSON 架构中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41837518/

相关文章:

ruby-on-rails - rails destroy scaffold leaves 后 table

sql - 在数据库中存储部分日期

sql - 如何为组 SQL Server 分配一个 id

java - Joda DateTime 从 JSON 反序列化为当前时间戳而不是提供的数据

java - 我需要java中的验证方法

c# - 如何发布对象列表?

json - 请求正文验证

asp.net - 使用 JavaScript 禁用 ASP.NET 验证器

iOS 刷新 JSON 表

ios - 无法在 uitableview 中解析 json 文件