在oneOf之前是否可以具有公共属性? [英] Is it possible to have common properties before oneOf?

查看:84
本文介绍了在oneOf之前是否可以具有公共属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json模式,该模式具有一个包含多个对象的数组,并且每个对象根据某些模式与其他对象略有不同.

I have this json schema that has an array that contains multiple object, and each object differs slightly from the others based on certain patterns.

示例.

[
  {
    "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和数组?

My issue is that although the real json is similar, it has many more items, and it is poised to grow larger as more time passes. Therefore I must ask, is it possible for the schema to group the identical elements Name, and Surname, and only have the ID and the arrays in the 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
}

推荐答案

通常,您想在前面定义常见的东西,之后再定义特殊的条件.这使架构更易于阅读,并产生更好的错误消息.

Generally, you want to define the common stuff upfront and the special conditions after. This makes the schema easier to read and results in better error messages.

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

In this example, if "ProjectsInvolved" is present, then "ID" must start with "pg" and "WorksOn" can not be present. And, if "WorksOn" is present, then "ID" must start with "jt" and "ProjectsInvolved" can not be present.

使用oneOfanyOf也可以进行类似的操作,但是通常使用dependencies可以获得更好的错误消息.

It is possible to something like this with oneOf or anyOf as well, but you generally get better error messaging with 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
}

这篇关于在oneOf之前是否可以具有公共属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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