json - 条件语句在 json 模式验证中不起作用
问题描述
我有一个如下所示的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"
]
}
}
}
}
]
}
}
}
}
}
}
}
}
推荐阅读
- python - dyld:库未加载:/usr/local/opt/gcc/lib/gcc/9/libquadmath.0.dylib
- r - R 中的 If Else 语句 - else 中的意外 else
- c# - Kentico 通过选定的 smtp 服务器发送电子邮件
- jquery - 如何使用巧克力 jquery 在容器中设置当前图像?
- reactjs - 尝试获取资源时出现 NetworkError。(Spring Boot / 安全 / React / Redux)
- c# - 如何使用 Node 和 C# 通过 TCP 向客户端发送一个简单的 UInt
- maven - 如何阻止maven下载源jar
- css - 每次组件重新渲染时如何生成一个随机数?
- angular - Angular Universal 中的 gzip
- acumatica - 无法更新案例中的原因下拉列表值