我有一个对象提供了一种资产版本的审计日志。它的一些属性(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
}
}
}
}
}
我错过了什么?
答案 0 :(得分:5)
这是我自己的错字...... constant
应该是const
。 :捂脸:
答案 1 :(得分:2)
根据草案7
应注意,对于具有单个元素的枚举,const仅仅是语法糖,因此以下内容是等效的:
{ "const": "United States of America" }
{ "enum": [ "United States of America" ] }
在某些渲染表单解决方案中使用default
键来选择单个选项时,有些人可能会觉得有用。
答案 2 :(得分:0)