【问题标题】:Is it possible to have common properties before oneOf?oneOf之前是否可以有共同的属性?
【发布时间】:2016-10-03 15:18:42
【问题描述】:

我有这个 json 模式,它有一个包含多个对象的数组,每个对象根据某些模式与其他对象略有不同。

示例。

[
  {
    "ID": "pgID",
    "Name": "John",
    "Surname": "Doe",
    "ProjectsInvolved": [
      "My Project",
      "My Project 2"
    ]
  },
  {
    "ID": "jtID",
    "Name": "John",
    "Surname": "Doe",
    "WorksOn": [
      "Monday",
      "Thursday"
    ]
  }
]

相应的 json 架构是:

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "type": "array",
  "items": {
    "oneOf": [
      {
        "type": "object",
        "properties": {
          "ID": {
            "type": "string",
            "pattern": "^(pg)\\w*$"
          },
          "Name": {
            "type": "string"
          },
          "Surname": {
            "type": "string"
          },
          "ProjectsInvolved": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      },
      {
        "type": "object",
        "properties": {
          "ID": {
            "type": "string",
            "pattern": "^(jt)\\w*$"
          },
          "Name": {
            "type": "string"
          },
          "Surname": {
            "type": "string"
          },
          "WorksOn": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    ]
  },
  "additionalProperties": false
}

我的问题是,虽然真正的 json 是相似的,但它有更多的项目,并且随着时间的推移它会变得更大。因此我必须问,模式是否可以将相同的元素 Name 和 Surname 分组,并且只有 ID 和 oneOf 中的数组?

建议架构的示例:

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "Name": {
        "type": "string"
      },
      "Surname": {
        "type": "string"
      },
      "oneOf": [
        {
          "ID": {
            "type": "string",
            "pattern": "^(pg)\\w*$"
          },
          "ProjectsInvolved": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        },
        {
          "ID": {
            "type": "string",
            "pattern": "^(jt)\\w*$"
          },
          "WorksOn": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      ]
    }
  },
  "additionalProperties": false
}

【问题讨论】:

    标签: json validation jsonschema


    【解决方案1】:

    一般来说,您想先定义常见的东西,然后定义特殊的条件。这使架构更易于阅读并产生更好的错误消息。

    在此示例中,如果存在“ProjectsInvolved”,则“ID”必须以“pg”开头,并且不能存在“WorksOn”。并且,如果存在“WorksOn”,则“ID”必须以“jt”开头,并且不能存在“ProjectsInvolved”。

    oneOfanyOf 也可能出现这种情况,但通常使用 dependencies 可以获得更好的错误消息。

    {
      "$schema": "http://json-schema.org/draft-04/schema",
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "ID": { "type": "string" },
          "Name": { "type": "string" },
          "Surname": { "type": "string" },
          "ProjectsInvolved": {
            "type": "array",
            "items": { "type": "string" }
          },
          "WorksOn": {
            "type": "array",
            "items": { "type": "string" }
          }
        },
        "dependencies": {
          "ProjectsInvolved": {
            "properties": {
              "ID": { "pattern": "^(pg)\\w*$" }
            },
            "not": { "required": ["WorksOn"] }
          },
          "WorksOn": {
            "properties": {
              "ID": { "pattern": "^(jt)\\w*$" }
            },
            "not": { "required": ["ProjectsInvolved"] }
          }
        }
      },
      "additionalProperties": false
    }
    

    【讨论】:

    • 正是我需要的!我希望我能给你一个额外的 +1 只是因为你的名字是 Jason 并且你正在回答一个与 JSON 相关的问题。 :D
    猜你喜欢
    • 1970-01-01
    • 2015-12-11
    • 2011-02-23
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2018-04-17
    相关资源
    最近更新 更多