JSON 架构 : Why does "constant" not validate the same way as a single-valued "enum"?

标签 json jsonschema

我有一个对象,它提供一种 Assets 版本的审计日志。它的几个属性( versionSource.metadataversionSource.files )是应该针对两个模式之一进行验证的对象,具体取决于它们的一个属性的值。我开始在我的子模式中使用一个常量(在 oneOf 内,但那是说所有子模式都经过验证(因此打破了 oneOf 因为不止一个验证。将其更改为单值不过,枚举有效。

为什么验证不同?

这是原始架构:

{
   "$id": "https://example.com/schemas/asset-version.json",
   "title": "Audit log of asset versions",
   "$schema": "http://json-schema.org/draft-07/schema",

   "type": "object",
   "required": [
      "assetID",
      "version",
      "versionSource"
   ],

   "properties": {
      "assetID": {
         "type": "string"
      },
      "version": {
         "type": "integer",
         "minimum": 1
      },
      "versionSource": {
         "type": "object",
         "properties": {
            "metadata": {
               "type": "object",
               "oneOf": [
                  {
                     "properties": {
                        "sourceType": { "constant": "client" }
                     }
                  },
                  {
                     "$ref": "#/definitions/version-source-previous-version"
                  }
               ]
            },
            "files": {
               "type": "object",
               "oneOf": [
                  {
                     "properties": {
                        "sourceType": { "constant": "upload" },
                        "sourceID": {
                           "type": "string"
                        }
                     }
                  },
                  {
                     "$ref": "#/definitions/version-source-previous-version"
                  }
               ]
            }
         }
      }
   },

   "definitions": {
      "version-source-previous-version": {
         "properties": {
            "sourceType": { "constant": "previous-version" },
            "sourceID": {
               "type": "integer",
               "minimum": 1
            }
         }
      }
   }
}

这是一个示例文档:
{
   "assetID": "0150a186-068d-43e7-bb8b-0a389b572379",
   "version": 1,
   "versionSource": {
      "metadata": {
         "sourceType": "client"
      },
      "files": {
         "sourceType": "upload",
         "sourceID": "54ae67b0-3e42-464a-a93f-3143b0f078fc"
      }
   },
   "created": "2018-09-01T00:00:00.00Z",
   "lastModified": "2018-09-02T12:10:00.00Z",
   "deleted": "2018-09-02T12:10:00.00Z"
}

还有一个:
{
   "assetID": "0150a186-068d-43e7-bb8b-0a389b572379",
   "version": 2,
   "versionSource": {
      "metadata": {
         "sourceType": "previous-version",
         "sourceID": 1
      },
      "files": {
         "sourceType": "previous-version",
         "sourceID": 1
      }
   },
   "created": "2018-09-01T00:00:00.00Z",
   "lastModified": "2018-09-02T12:10:00.00Z",
   "deleted": "2018-09-02T12:10:00.00Z"
}

这是我得到的错误:

消息:JSON 对来自“oneOf”的多个架构有效。有效的架构索引:0、1。
架构路径:
https://example.com/schemas/asset-version.json#/properties/versionSource/properties/metadata/oneOf

sourceTypeoneOf 中的两个模式中都是常量,我真的不确定我的对象如何可能对两种模式都有效。

但是,将架构更改为以下内容有效:
{
   "$id": "https://example.com/schemas/asset-version.json",
   "title": "Audit log of asset versions",
   "$schema": "http://json-schema.org/draft-07/schema",

   "type": "object",
   "required": [
      "assetID",
      "version",
      "versionSource"
   ],

   "properties": {
      "assetID": {
         "type": "string"
      },
      "version": {
         "type": "integer",
         "minimum": 1
      },
      "versionSource": {
         "type": "object",
         "properties": {
            "metadata": {
               "type": "object",
               "oneOf": [
                  {
                     "properties": {
                        "sourceType": { "enum": [ "client" ] }
                     }
                  },
                  {
                     "$ref": "#/definitions/version-source-previous-version"
                  }
               ]
            },
            "files": {
               "type": "object",
               "oneOf": [
                  {
                     "properties": {
                        "sourceType": { "enum": [ "upload" ] },
                        "sourceID": {
                           "type": "string"
                        }
                     }
                  },
                  {
                     "$ref": "#/definitions/version-source-previous-version"
                  }
               ]
            }
         }
      }
   },

   "definitions": {
      "version-source-previous-version": {
         "properties": {
            "sourceType": { "enum": [ "previous-version" ] },
            "sourceID": {
               "type": "integer",
               "minimum": 1
            }
         }
      }
   }
}

我错过了什么?

最佳答案

这是我自己的错别字... constant应该是const . :脸掌:

关于JSON 架构 : Why does "constant" not validate the same way as a single-valued "enum"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48389997/

相关文章:

java - 从 JSON 文件中获取数据,保存为对象并使用键值获取数据

java - 带有 gradle 的 Jersey 测试框架 - MessageBodyProviderNotFound

java - 如何使用 Hibernate\Spring Data 将 json 转换为 java 对象数据并保存到数据库中?

用于数据描述与数据验证与输入验证的 JSON 模式

c# - 将 JSON 对象 A 转换为 JSON B 对象,其中 B 是 A 的严格子集。两者都由两个 json 模式管理。在 .net 核心中

JSON Schema - 基于所选值的下拉列表其他下拉列表

json - 如何显示字符而不是ascii?

c# - 反序列化来自 firebase 的响应

json - 您如何在 JSON 模式中引用其他属性值?

javascript - 使用 Electron 商店时来自 ajv 的严格模式警告