如何使用 $ref 文件中的附加属性?

how to use additionalProperties from a $ref file?

我将我的 JSON 模式分成两个文件。

person-input.json(将由输入设置的所有属性。)

person.json(保留对 person-input.json 的引用,但也有 dateUpdate、dateCreated 和 DateDeleted)。

这是为了将输入与自动生成的日期属性分开。

我不希望任何帖子能够向我的数据添加不需要的属性,所以我想我会使用 "additionalProperties": false 问题是如果我在 person-input.json 文件中使用它不会接受 person.json 文件中我的 "date" 属性。如果我把它放在 person.json 文件中,它不会阻止添加随机属性。有针对这个的解决方法吗?

所以下面这个不起作用,我是不是放错了 "additionalProperties": false

person.json

{
  "allOf": [
    {
      "$ref": "./person-input.json"
    },
    {
      "type": "object",
      "properties": {
        "dateCreated": {
        "name": "dateCreated",
        "type": "string",
        "description": "date created",
        "example": "2019-09-02T11:17:41.783Z"
        },
        "dateUpdated": {
          "type": "string",
          "nullable": true,
          "description": "date updated",
          "example": "2019-09-02T11:17:41.783Z"
        },
        "dateDeleted": {
          "type": "string",
          "nullable": true,
          "description": "date deleted",
          "example": "2019-09-02T11:17:41.783Z"
        }
      },
      "additionalProperties": false
    }
  ]
}

additionalProperties 不能像 allOf 那样“看穿”涂抹器,也不能“看穿”使用 $ref.

为了解决这个问题,您必须在最外层/最顶层模式中对模式进行一些复制,并从任何子模式中删除 additionalProperties: false 要求。

additionalProperties: false 通过将 false(这是一个有效的模式,returns 无法验证)应用于与基于 properties 的键不匹配的值或patternProperties 在 SAME 架构对象中。

Validation with "additionalProperties" applies only to the child
values of instance names that do not match any names in "properties", and do not match any regular expression in "patternProperties".

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.5.6(草案 7)

因此,如果您需要将所需的所有属性复制到顶级架构。是的,这不好!

您可以通过观察 properties 对象的值是模式这一事实使其变得更好一点,因此可能只是 true,允许子模式实际执行稍后验证。

这是我将在即将进行的演讲中使用的示例:

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "title": "MatchMakerExchange format for queries",
  "definitions": {
    "phenotypicFeatures": {
      "type": [
        "array"
      ]
    },
    "genomicFeatures": {
      "type": [
        "array"
      ]
    },
    "geneticsPatient": {
      "properties": {
        "phenotypicFeatures": {
          "$ref": "#/definitions/phenotypicFeatures"
        },
        "genomicFeatures": {
          "$ref": "#/definitions/genomicFeatures"
        }
      },
      "anyOf": [
        {
          "required": [
            "phenotypicFeatures"
          ]
        },
        {
          "required": [
            "genomicFeatures"
          ]
        }
      ]
    },
    "regularPatient": {
      "type": "object",
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "type": [
            "string"
          ]
        }
      }
    }
  },
  "properties": {
    "patient": {
      "additionalProperties": false,
      "properties": {
        "name": true,
        "phenotypicFeatures": true,
        "genomicFeatures": true
      },
      "allOf": [
        {
          "$ref": "#/definitions/regularPatient"
        },
        {
          "$ref": "#/definitions/geneticsPatient"
        }
      ]
    }
  }
}

您可能会问...“好吧,这太疯狂了。请问您能解决这个问题吗?” - 我们做到了。它称为草稿 2019-09,它最近才发布,因此您必须等待实现赶上进度。

新关键字 unevaluatedProperties 取决于注释结果,但您仍然需要从子架构中删除 additionalProperties: false