json - 如何使用 $ref 文件中的additionalProperties?

标签 json jsonschema openapi

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

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

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

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

我不希望任何帖子能够向我的数据添加不需要的属性,所以我想我会使用 "additionalProperties": false 问题是,如果我在 中使用它>person-input.json 文件,它不会接受 person.json 文件中的“日期”属性。如果我将其放入 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 (这是一个有效的架构,返回验证失败)应用于与基于 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,最近才发布,因此您必须等待实现 catch 。

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

关于json - 如何使用 $ref 文件中的additionalProperties?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58170135/

相关文章:

javascript - 如何根据所选值从 JSON 数组构建列表?

json - 在Windows Excel VBA中,如何获取JSON key 来抢占 "Run-time error ' 43 8': Object doesn' t支持此属性或方法”?

amazon-web-services - AWS API 网关 : Documentation Swagger export model type null ignored

swagger - 在 NestJS 中,我可以在 Controller 级别添加什么装饰器以将 Authorization header 添加到我的 Swagger 文档?

json - 经典 ASP xmlhttp 获取如何为 sendgrid api 添加身份验证

python - 类型错误 : It would appear that nargs is set to conflict with the composite type arity

json - 属性尚未定义,架构不允许附加属性

树结构的 JSON 模式

api - 取决于 OpenApi 3.0.0/Swagger 中的媒体类型的多个响应正文示例

spring - 使用 Spring Boot 自定义 OpenApi 文档