我有一个如下的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
瓷砖,我必须验证以下关键要素的存在
"context"
"collection"
"tile_type"
对于scrollable
瓷砖,我必须验证以下关键要素的存在
"name"
"location"
"tile_type"
基于这些,我使用这样的开关定义了一个模式,并且模式验证不起作用。switch
我也尝试过使用关键字anyOf
代替(我使用的是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
这是一个使用以下验证规则适用于给定JSON实例的模式:
类型可以是static
或scrollable
如果类型是static
,在最大一个项目tiles
阵列,和对象属性必须是context
,collection
,和tile_type
。
如果类型是scrollable
,在至少两个项目tiles
阵列,和对象属性必须是name
,location
,和tile_type
。
磁scrollable
贴中的项目必须唯一。
除此之外,瓷砖的元素也有所不同
抱歉,JSON Schema无法实现。
还使用您使用的同一在线验证器进行了测试。
{
"type": "array",
"items": {
"properties": {
"views": {
"type": "array",
"items": {
"properties": {
"groups": {
"type": "array",
"items": {
"oneOf": [
{
"properties": {
"type": {
"const": "static"
},
"tiles": {
"type": "array",
"maxItems": 1,
"items": {
"propertyNames": {
"enum": [
"context",
"collection",
"tile_type"
]
},
"required": [
"context",
"collection",
"tile_type"
]
}
}
}
},
{
"properties": {
"type": {
"const": "scrollable"
},
"tiles": {
"type": "array",
"minItems": 2,
"items": {
"propertyNames": {
"enum": [
"name",
"location",
"tile_type"
]
},
"required": [
"name",
"location",
"tile_type"
]
}
}
}
}
]
}
}
}
}
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句