对于JSON模式,使用“allOf”作为引用定义是否更好?

k2fxgqgv  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(155)

在JSON模式中,当一个属性引用另一个已定义的模式时,为了保留被引用定义的类型,是否最好将被引用的定义 Package 在“allOf”中,并在被引用定义之外的该定义中指示属性类型?或者在引用的定义中指明属性类型而不使用“allOf”是否更好?
例如,在下面的第一个模式中,mainObject和objectA的属性引用都 Package 在“allOf”和“type”中:在mainObject和objectA属性中使用“object”。而在模式版本2中,“mainObject”和“objectAInfo”定义指示“object”的“类型”。
从我在测试中看到的情况来看,这两个版本都实现了相同的验证目标,所以我想知道是否有特别的理由使用模式版本1而不是模式版本2?
架构版本1:

{
  "info": {
    "title": "mainObject",
    "version": "0.0.1",
    "baseUri": "",
    "description": "An example object schema"
  },
  "properties": {
    "mainObject": {
      "type": "object",
      "allOf": [
        {
          "$ref": "#/definitions/mainObject"
        }
      ]
    }
  },
  "required": [],
  "definitions": {
    "mainObject": {
      "additionalProperties": false,
      "description": "The financials entity root.",
      "required": [],
      "properties": {
        "objectA": {
          "type": "object",
          "description": "Information on the liability accrued by the taxpayer",
          "allOf": [
            {
              "$ref": "#/definitions/objectAInfo"
            }
          ]
        }
      }
    },
    "objectAInfo": {
      "additionalProperties": false,
      "required": [],
      "properties": {
        "total1": {
          "type": "number",
          "description": "Overall liability accrued during the tax period. This number does not go down as the taxpayer pays off their liability. That is reflected in the periodBalance."
        },
        "total2": {
          "type": "number",
          "description": "Overall liability accrued during the tax period. This number does not go down as the taxpayer pays off their liability. That is reflected in the periodBalance."
        }
      }
    }
  },
  "$schema": "http://json-schema.org/draft-07/schema#",
  "lang": "zxx",
  "$id": "http://example.com/"
}

架构版本2:

{
  "info": {
    "title": "mainObject",
    "version": "0.0.1",
    "baseUri": "",
    "description": "An example object schema"
  },
  "properties": {
    "mainObject": {
      "$ref": "#/definitions/mainObject"
    }
  },
  "required": [],
  "definitions": {
    "mainObject": {
      "type": "object",
      "additionalProperties": false,
      "description": "The financials entity root.",
      "required": [],
      "properties": {
        "objectA": {
          "$ref": "#/definitions/objectAInfo"
        }
      }
    },
    "objectAInfo": {
      "type": "object",
      "additionalProperties": false,
      "required": [],
      "properties": {
        "total1": {
          "type": "number",
          "description": "Overall liability accrued during the tax period. This number does not go down as the taxpayer pays off their liability. That is reflected in the periodBalance."
        },
        "total2": {
          "type": "number",
          "description": "Overall liability accrued during the tax period. This number does not go down as the taxpayer pays off their liability. That is reflected in the periodBalance."
        }
      }
    }
  },
  "$schema": "http://json-schema.org/draft-07/schema#",
  "lang": "zxx",
  "$id": "http://example.com/"
}

要验证的文档示例:

{
  "mainObject": {
    "objectA": {
      "total1": 0,
      "total2": 1
    }
  }
}
3j86kqsm

3j86kqsm1#

这取决于三件事:
1.你用的是哪种草稿?草案7和更早的版本 * 要求 * $ref是独立的,这意味着如果您有其他约束(例如,Schema 1中的type),它们将被忽略。然而,在2019-09及以后的草案中,$ref * 可以 * 有兄弟关键字(type也将被验证)。
1.多次引用。如果你有多个引用,你必须把它们放在allOf中单独的子模式中。否则你会有重复的$ref键。
1.可读性。即使对于现代模式,有些人仍然喜欢引用本身,以帮助他们隔离不同的需求集。这是个人意见。
简短回答:对于现代模式实现,这取决于您。只要确保声明您使用的$schema版本即可。

相关问题