【问题标题】:Conditional statement is not working in json schema validation条件语句在 json 模式验证中不起作用
【发布时间】:2018-08-31 11:12:14
【问题描述】:

我有一个如下所示的 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 键,tile 中的大小和键元素 对象不同。例如,如果type 键为static,则tile 对象的大小为1,如果它的值为scrollable,则它包含多个 tile 项目。除此之外tile 元素也不同。

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

                          "context"
                        "collection"                            
                        "tile_type"

对于scrollable tile 我必须验证以下关键元素的存在

                        "name"
                        "location"
                        "tile_type"

基于这些,我已经使用这样的开关定义了一个架构,但架构验证不起作用。我也尝试使用 anyOf 代替 switch 关键字。(我使用的是 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

【问题讨论】:

  • 删除问题并重新发布是不好的形式,因为它已被锁定并需要编辑! switch 不是 JSON Schema 关键字。
  • 为什么我删除了那个问题是有人建议删除那个帖子并处于搁置状态。因此我的问题对任何人都不可见。这促使我删除了该问题并在此处重新发布。跨度>
  • switch 被提议但从未同意,我们改为使用 if/then/else。我知道,我是 JSON Schema 核心团队的一员。 switch 不在规范中,因此该关键字的任何使用都将是特定于实现的。
  • 在使用Of关键字方面,现在您已经提供了您尝试过的内容,我应该可以更好地提供帮助!待机 =]
  • 您找到的架构定义是如何使用它的示例,如果它包含在 JSON 架构规范中。这是一个提议。我们选择不允许它。

标签: json jsonschema json-schema-validator


【解决方案1】:

这是一个适用于给定 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"
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}

【讨论】:

  • 这可以通过 if/then/else 来实现,但是使用 oneOf 也可以。
  • 很高兴回答您可能对此提出的任何问题!
  • 不客气! enum 与一个项目也将有效,const 是一个快捷方式。
  • 当然。 required 只检查属性是否存在。如果您要设置additionalProperties: false,它将无法通过验证,因为该架构不使用properties,因此所有属性都是“附加属性”。使用propertyNames 确保属性只是那些,并且没有其他属性是有效的。您可以使用properties 为每个属性添加定义,然后使用additionalProperties: false,但这不是您想要的,而且更冗长。
  • Validation with "additionalProperties" applies only to the child values of instance names that do not match any names in "properties", and do not match any regular expression in "patternProperties". - tools.ietf.org/html/…
猜你喜欢
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
  • 2015-05-16
  • 2021-10-16
  • 1970-01-01
相关资源
最近更新 更多