oneOf之前是否可以有共同的属性?

Is it possible to have common properties before oneOf?

我有这个 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 分组,并且在 oneOf 中只有 ID 和数组?

建议架构示例:

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

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

在此示例中,如果存在 "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
}