条件语句在 json 模式验证中不起作用
Conditional statement is not working in json schema validation
我有一个 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
对象中每个 group
array.Based 中 type
键上的 tile
对象的大小和关键元素在 tile
对象不同。例如,如果 type
键是 static
,tile
对象的大小是 1
,如果它的值是 scrollable
,它包含多个
tile
items.In 除此之外 tile
元素也不同。
对于 static
图块,我必须验证是否存在以下关键元素
"context"
"collection"
"tile_type"
对于 scrollable
图块,我必须验证是否存在以下关键元素
"name"
"location"
"tile_type"
基于这些,我使用这样的开关定义了一个模式,并且模式验证不是 switch
关键字的 working.Instead 我也尝试使用 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
数组中添加了一个额外的属性,如下所示。
但是我的模式定义 working.Whats 并没有错
{
"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
拼贴中的项目必须是唯一的。
In addition to this the tile elemnts also different
抱歉,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"
]
}
}
}
}
]
}
}
}
}
}
}
}
}
我有一个 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
对象中每个 group
array.Based 中 type
键上的 tile
对象的大小和关键元素在 tile
对象不同。例如,如果 type
键是 static
,tile
对象的大小是 1
,如果它的值是 scrollable
,它包含多个
tile
items.In 除此之外 tile
元素也不同。
对于 static
图块,我必须验证是否存在以下关键元素
"context"
"collection"
"tile_type"
对于 scrollable
图块,我必须验证是否存在以下关键元素
"name"
"location"
"tile_type"
基于这些,我使用这样的开关定义了一个模式,并且模式验证不是 switch
关键字的 working.Instead 我也尝试使用 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
数组中添加了一个额外的属性,如下所示。
但是我的模式定义 working.Whats 并没有错
{
"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
拼贴中的项目必须是唯一的。
In addition to this the tile elemnts also different
抱歉,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"
]
}
}
}
}
]
}
}
}
}
}
}
}
}