我有一个如下的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
键,{{ 1}}
对象有所不同。为了简便起见,如果tile
键是type
,则static
对象的大小是tile
,如果它的值是1
,则意味着多个
scrollable
个项目。除此之外,tile
个元素也有所不同。
对于tile
磁贴,我必须验证以下关键元素的存在
static
对于 "context"
"collection"
"tile_type"
磁贴,我必须验证以下关键元素的存在
scrollable
基于这些,我已经使用类似这样的开关定义了一个架构,并且架构验证不起作用。我也尝试使用 "name"
"location"
"tile_type"
来代替switch
关键字。(我使用的是draft7版本)< / p>
模式定义
anyOf
尝试了anyOF
"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"
]
}
]
尝试了:https://www.jsonschemavalidator.net/
执行此操作的任何解决方案?
更新的部分位于以下
在响应中,有时某些 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
数据包含键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"
]
}
}
}
}
答案 0 :(得分:2)
以下是适用于您的给定JSON实例的架构,该架构具有以下验证规则:
类型可以为static
或scrollable
如果类型为static
,则tiles
数组中最多只能有一项,并且对象属性必须为context
,collection
和tile_type
。
如果类型为scrollable
,则tiles
数组中至少有两项,并且对象属性必须为name
,location
和tile_type
。
scrollable
磁贴中的项目必须唯一。
除此之外,图块元素也不同
对不起,这对于JSON模式是不可能的。
还使用了与您使用的相同的在线验证器进行了测试。
{
"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"
]
}
}
}
}
]
}
}
}
}
}
}
}
}