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

divz

我有一个如下的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的大小tile1,值是值是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数据有时包含键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如果类型是static,在最大一个项目tiles阵列,和对象属性必须是contextcollection,和tile_type

如果类型是scrollable,在至少两个项目tiles阵列,和对象属性必须是namelocation,和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"
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章