【发布时间】:2018-08-31 11:12:14
【问题描述】:
我有一个如下所示的 json 响应..
[{
"views": [{
"groups": [{
"type": "static",
"tiles": [{
"context": "event",
"collection": "selo",
"tile_type": "static"
}
]
}
]
}, {
"groups": [{
"type": "static",
"tiles": [{
"context": "event",
"collection": "nitic",
"tile_type": "static"
}
]
}
]
}, {
"groups": [{
"type": "scrollable",
"tiles": [{
"name": "loca",
"location": "cal",
"tile_type": "person"
}, {
"name": "tom",
"location": "toc",
"tile_type": "person"
}
]
}
]
}
]
}
]
这里我必须验证每个group 数组中的tile 对象。基于group 对象中的type 键,tile 中的大小和键元素
对象不同。例如,如果type 键为static,则tile 对象的大小为1,如果它的值为scrollable,则它包含多个
tile 项目。除此之外tile 元素也不同。
对于static tile 我必须验证以下关键元素的存在
"context"
"collection"
"tile_type"
对于scrollable tile 我必须验证以下关键元素的存在
"name"
"location"
"tile_type"
基于这些,我已经使用这样的开关定义了一个架构,但架构验证不起作用。我也尝试使用 anyOf 代替 switch 关键字。(我使用的是 draft7 版本)
模式定义
"switch": [
{
"if": {
"properties": {
"tile_type": {
"enum": [
"static"
]
}
},
"required": [
"context",
"collection",
"tile_type"
]
}
},
{
"if": {
"properties": {
"tile_type": {
"enum": [
"person"
]
}
},
"required": [
"name",
"location",
"tile_type"
]
}
}
]
尝试使用 anyOF
"anyOf": [{
"properties": {
"tile_type": {
"enum": [
"static"
]
}
},
"required": [
"context",
"collection",
"tile_type"
]
}, {
"properties": {
"tile_type": {
"enum": [
"person"
]
}
},
"required": [
"name",
"location",
"tile_type"
]
}
]
使用 anyOf 时观察到的错误
Found 2 error(s)
Message:
Required properties are missing from object: context, collection.
Schema path:
http://example.com/root.json#/views/groups/tiles/items/required
Message:
Required properties are missing from object: context, collection.
Schema path:
http://example.com/root.json#/views/groups/tiles/items/required
试用:https://www.jsonschemavalidator.net/
有什么解决方案来执行此操作?
更新部分如下
在响应中,有时某些tile 数据包含键errorText 和errorCode。
[{
"views": [{
"groups": [{
"type": "static",
"tiles": [{
"context": "event",
"tile_type": "static"
}
]
}
]
}, {
"groups": [{
"type": "static",
"tiles": [{
"context": "event",
"collection": "nitic",
"tile_type": "static"
}
]
}
]
}, {
"groups": [{
"type": "scrollable",
"tiles": [{
"name": "loca",
"location": "cal",
"tile_type": "person"
}, {
"errorText":"Tile failure",
"errorCode":1,
"tile_type": "person"
},
{
"errorText":"Tile not generated",
"errorCode":2,
"tile_type": "person"
}
]
}
]
}
]
}
]
在这种情况下,我在现有的oneOf 数组中添加了一个额外的属性,如下所示。
但它不起作用。我的架构定义有什么问题
{
"properties": {
"type": {
"const": "scrollable"
},
"tiles": {
"type": "array",
"minItems": 2,
"items": {
"properties": {
"errorText": {
"const": ["Tile failure", "Tile not generated"]
}
},
"required": [
"errorText",
"errorCode",
"tile_type"
]
}
}
}
}
进行架构验证时的错误消息:
Message:
Value "static" does not match const.
Schema path:
#/items/properties/views/items/properties/groups/items/oneOf/2/properties/type/const
Message:
Value "static" does not match const.
Schema path:
#/items/properties/views/items/properties/groups/items/oneOf/1/properties/type/const
【问题讨论】:
-
删除问题并重新发布是不好的形式,因为它已被锁定并需要编辑!
switch不是 JSON Schema 关键字。 -
为什么我删除了那个问题是有人建议删除那个帖子并处于搁置状态。因此我的问题对任何人都不可见。这促使我删除了该问题并在此处重新发布。跨度>
-
switch被提议但从未同意,我们改为使用 if/then/else。我知道,我是 JSON Schema 核心团队的一员。switch不在规范中,因此该关键字的任何使用都将是特定于实现的。 -
在使用
Of关键字方面,现在您已经提供了您尝试过的内容,我应该可以更好地提供帮助!待机 =] -
您找到的架构定义是如何使用它的示例,如果它包含在 JSON 架构规范中。这是一个提议。我们选择不允许它。
标签: json jsonschema json-schema-validator