条件语句在json架构验证中不起作用

条件语句在json架构验证中不起作用,json,jsonschema,json-schema-validator,Json,Jsonschema,Json Schema Validator,我有一个json响应,如下所示 [{ "views": [{ "groups": [{ "type": "static", "tiles": [{ "context": "event",

我有一个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"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
在这里,我必须验证每个
数组中的
平铺
对象。根据
对象中的
类型
键,
平铺中的大小和关键元素
对象不同。对于isntance,如果
类型
键为
静态
平铺
对象的大小为
1
,如果值为可滚动
则包含多个
tile
项目。除此之外,
tile
元素也不同

对于
static
tile,我必须验证是否存在以下关键元素

                          "context"
                        "collection"                            
                        "tile_type"
                        "name"
                        "location"
                        "tile_type"
对于可滚动的
磁贴,我必须验证是否存在以下关键元素

                          "context"
                        "collection"                            
                        "tile_type"
                        "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"
                        ]
                      }
                    }
                  ]
 {
                    "properties": {
                      "type": {
                        "const": "scrollable"
                      },
                      "tiles": {
                        "type": "array",
                        "minItems": 2,
                        "items": {
                          "properties": {
                               "errorText": {
                              "const": ["Tile failure", "Tile not generated"]
                           }
                          },
                          "required": [
                            "errorText",
                            "errorCode",
                            "tile_type"
                          ]
                        }
                      }
                    }
                  }
试过了吗

"anyOf": [{
        "properties": {
            "tile_type": {
                "enum": [
                    "static"
                ]
            }

        },
        "required": [
            "context",
            "collection",
            "tile_type"
        ]

    }, {

        "properties": {
            "tile_type": {
                "enum": [
                    "person"
                ]
            }
        },
        "required": [
            "name",
            "location",
            "tile_type"
        ]

    }
]
使用任意一个时发现的错误

 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
试用期:

有什么解决方案来执行此操作吗

更新部分如下所示

在响应中,有时一些
平铺
数据包含键
errorText
errorCode

[{

]

在这种情况下,我在现有的
oneOf
数组中添加了一个额外的属性,如下所示。 但它不起作用。我的模式定义有什么问题吗

   "switch": [
                    {
                      "if": {
                        "properties": {
                          "tile_type": {
                            "enum": [
                              "static"
                            ]
                          }

                        },
                        "required": [
                          "context",
                          "collection",
                          "tile_type"
                        ]
                      }
                    },
                    {
                      "if": {
                        "properties": {
                          "tile_type": {
                            "enum": [
                              "person"
                            ]
                          }
                        },
                        "required": [
                          "name",
                          "location",
                          "tile_type"
                        ]
                      }
                    }
                  ]
 {
                    "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实例,具有以下验证规则:

类型可以是
静态
可滚动
如果类型为
静态
,则
平铺
数组中最多有一项,并且对象属性必须为
上下文
集合
、和
平铺类型

如果类型是可滚动的,则
平铺
数组中至少有两个项目,并且对象属性必须是
名称
位置
,和
平铺类型

可滚动
磁贴中的项目必须是唯一的

除此之外,瓷砖元素也不同

很抱歉,这在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"
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}

删除一个问题并重新发布是不好的,因为它被锁定并且需要编辑!!
switch
不是一个JSON模式关键字。我删除该问题的原因是有人建议删除该帖子并处于保留状态。因此我的问题对任何人都不可见。这促使我删除该问题并在此处重新发布。
switch
被提出,但从未达成一致,我们选择了if/then/else。我知道,我是JSON模式核心团队的一员。
switch
不在规范中,因此该关键字的任何使用都将是特定于实现的。就使用
of
关键字而言,现在您已经提供了您已经尝试过的内容,我应该能够更好地帮助您p!Standby=]您找到的模式定义是如何在JSON模式规范中使用它的示例。这是一个建议。我们选择不允许它。这可以通过if/then/else实现,但是使用
oneOf
也可以。很高兴回答您对此可能提出的任何问题!不客气!
enum
如果有一个项目也会有效,
const
是一个快捷方式。当然。
required
只检查属性是否存在。如果要设置
additionalProperties:false
,则验证将失败,因为该架构不使用
属性,因此所有属性都是“additionalProperties”。使用
propertyNames
可确保属性仅为这些属性,并且没有其他有效属性。您可以使用
properties
为每个属性添加定义,然后使用
additionalProperties:false
,但这不是您想要的,而且更详细。
使用“additionalProperties”验证仅适用于与“属性”中的任何名称不匹配的实例名称的子值,以及与“模式属性”中的任何正则表达式不匹配的实例名称的子值。
-