首页 > 解决方案 > 条件语句在 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的大小,如果是值,则它包含多个 项目。除此之外,元素也不同。tile1scrollabletiletile

对于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数据包含键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

标签: jsonjsonschemajson-schema-validator

解决方案


这是一个适用于给定 JSON 实例的模式,具有以下验证规则:

类型可以是staticscrollable 如果类型是static,则数组中最多一项tiles,并且对象属性必须是contextcollectiontile_type

如果类型为scrollable,则数组中至少有两项tiles,并且对象属性必须为namelocationtile_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"
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}

推荐阅读