>我有一个对象,它提供了资产版本的一种审核日志。它的几个属性(versionSource.metadata
和versionSource.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
由于sourceType
在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": { "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
.:脸掌:
根据草案 7
应该注意的是,const 只是具有单个元素的枚举的语法糖,因此以下内容是等价的:
{ "const": "United States of America" }
{ "enum": [ "United States of America" ] }
在某些渲染表单解决方案中使用时,有些人可能会发现提供default
键以选择该单一选项很有用。
嗯,没有什么是错误的。 由于您使用的是draft-07,因此您可以尝试使用if/then/else编写它,看看错误是否更有帮助。
但。。。
您确定您使用的实现理解 draft-07 吗?如果它忽略$schema并通过 draft-04 规则运行它,它将无法理解const
.您应该查看您的工具文档。