我有一个像下面的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"
}
]
}
]
}
]
}
]
在这里,我必须验证每个tile
数组中的group
对象。基于type
对象中的group
键,tile
对象中的大小和关键元素各不相同。如果type
关键是static
,那么tile
对象的大小是1
,如果值是scrollable
它会包含多个tile
项目。除此之外,tile
元素也不同。
对于static
tile我必须验证以下关键元素的存在
"context"
"collection"
"tile_type"
对于scrollable
tile我必须验证以下关键元素的存在
"name"
"location"
"tile_type"
基于这些,我已经使用这样的开关定义了一个模式,并且模式验证不起作用。而不是switch
关键字我也尝试使用anyOf
。(Iam使用draft7版本)
架构定义
"switch": [
{
"if": {
"properties": {
"tile_type": {
"enum": [
"static"
]
}
},
"required": [
"context",
"collection",
"tile_type"
]
}
},
{
"if": {
"properties": {
"tile_type": {
"enum": [
"person"
]
}
},
"required": [
"name",
"location",
"tile_type"
]
}
}
]
尝试任何OF
"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