json - 将必填字段应用于引用的 JSON 数据架构

标签 json reference jsonschema

我尝试使用 JSON 模式解决以下用例。

我有一个通用的 JSON 数据架构,例如用户的通用 JSON 数据架构。以下是 user.schema.json 文件的示例。

{
  "type": "object",
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 1
    },
    "email": {
      "type": "string",
      "minLength": 1
    },
    "locale": {
      "type": "string",
      "minLength": 1
    },
    "active": {
      "type": "boolean",
      "default": true
    },
    "password": {
      "type": "string",
      "minLength": 8
    },
    "roles": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      }
    }
  }
}

现在我有两种不同类型的请求: - POST:添加用户 - PATCH:更新用户数据。

在一种情况下,我可以发送此数据结构,其中包含 3 个必填字段,而在补丁的情况下,每个字段都是可选的。 所以我得到了post请求文件:post-user.schema.json:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "user.schema.json",
  "required": [
    "name",
    "password",
    "email"
  ]
}

对于我的补丁(path-user.schema.json:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "user.schema.json"
}

现在我遇到的问题是我的 POST 架构还标记了一个用户,例如:

{
    "name": "NoPassword",
    "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="254b4a55445656524a574165484a47440b4b49" rel="noreferrer noopener nofollow">[email protected]</a>",
    "roles": []
}

缺少必需的密码字段,作为有效的 JSON 架构。

显然,这不是将所需字段分配给引用的数据结构的方法。我尝试使用谷歌来查看我可以使用以下搜索找到有关此主题的内容: [如何将必填字段分配给引用的架构] 我尝试从文档中获取此信息。

我运气不好。

我现在的问题是: A. 是否可以将必填字段分配给 $referenced json 架构数据对象。 B. 如果可能的话该怎么做 C. 如果这是不可能的,那么解决这个问题的好方法是什么。

非常感谢任何帮助。

最佳答案

使用 $ref 会导致对象中的所有其他属性被忽略,因此您需要包装 $ref 的使用。

让我们看一下规范:

An object schema with a "$ref" property MUST be interpreted as a
"$ref" reference. The value of the "$ref" property MUST be a URI
Reference. Resolved against the current URI base, it identifies the
URI of a schema to use. All other properties in a "$ref" object MUST be ignored.

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-01#section-8.3

然后考虑您问题中包含的架构:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "user.schema.json",
  "required": [
    "name",
    "password",
    "email"
  ]
}

阅读规范,您可以明白为什么 required 将被忽略。

最初$ref仅设计用于替换整个对象,而不是添加到对象的条件。

您想要的是将多个架构应用于实例。为此,您可以使用 allOf

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "allOf": [
    {
      "$ref": "user.schema.json"
    },
    {
      "required": [
        "name",
        "password",
        "email"
      ]
    }
  ]
}

我将此模式加载到演示中供您在 https://jsonschema.dev 进行测试- 虽然它还不支持引用,所以我嵌入了引用,但验证将同样工作。

从草案 8 开始,$ref 将按照您的预期运行,因为它成为应用程序关键字而不是具有特殊行为的关键字,这意味着同一对象中的其他关键字不需要被忽略。

关于json - 将必填字段应用于引用的 JSON 数据架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57198531/

相关文章:

json - 需要帮助使用 Alamofire 使用 Swift 解析 JSON

c++ - 我有一个引用,想调用一个采用 boost::shared_ptr 的函数

python - JSON 架构 : Validate that exactly one property is present

yaml - Swagger 模式中的 oneOf 不起作用

javascript - 从表单更新 JSON 数组

javascript - 使用jquery使用选定的列从html表创建json对象

jquery - 为什么数据还是空

android - 如何在 Android 中引用其他应用程序的 R.java,如 com.android.music?

c++ - C++中的引用是如何用指针实现的

java - 数据引用在everit-org/json-schema库中被视为数字以验证JSONSchema