我有一个如下所示的 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
键code> object tile
中的大小和关键元素
对象不同。例如,如果 type
键是 static
,则 tile
对象的大小是 1
,如果它的值为scrollable
它包含多个
tile
项。除此之外,tile
元素也不同。
对于 static
tile,我必须验证以下关键元素的存在
"context"
"collection"
"tile_type"
对于 scrollable
tile 我必须验证以下关键元素的存在
"name"
"location"
"tile_type"
基于这些,我已经使用这样的开关定义了一个模式,但模式验证不起作用。我也尝试使用 anyOf
而不是 switch
关键字。(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"
]
}
}
]
尝试过 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
。
可滚动
磁贴中的项目必须是唯一的。
In addition to this the tile elemnts also different
抱歉,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"
]
}
}
}
}
]
}
}
}
}
}
}
}
}
关于json - 条件语句在 json 模式验证中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52114243/