json - 条件语句在 json 模式验证中不起作用

标签 json jsonschema json-schema-validator

我有一个如下所示的 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 数据包含键 errorTexterrorCode

[{

    "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 实例的架构,具有以下验证规则:

类型可以是staticscrollable 如果类型是statictiles 数组中最多一个项目,并且对象属性必须是contextcollectiontile_type.

如果类型是scrollabletiles数组中至少有两项,对象属性必须是namelocationtile_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/

相关文章:

java - 导出到 json 时生成的数据库实体的循环依赖

jsonschema - Json 模式动态 key 验证

json - 是否可以在JSON模式中定义两个属性之间的约束

json - 如何清理进入 Sinatra 应用程序的所有参数?

json - 可以用其他信息扩展avro json吗?

java - jsonschema2pojo maven插件不生成Java类

.net - 从 JSON Schema 生成 C# 类

mysql - Json 模式字段顺序

java - 可以通过对类型的引用来声明您的 JSON 架构吗?

javascript - 当鼠标离开浏览器时停止保存到 JSON 文件