提问者:小点点

使用条件语句的JSON模式定义


Iam试图使用json模式条件语句(使用草稿7)定义可选条件

我有一个这样的json响应。

[{
    "views": [{
            "name": "RSO Roster",
            "displayOrder": 5,
            "groups": [{
                    "type": "scrollable",
                    "displayOrder": 1,
                    "auditType": "player-pregame_roster",
                    "tiles": [{
                            "context": "event",
                            "dataStamp": 1535184247,
                            "tile_type": "person"
                        }, {
                            "context": "event",
                            "errorCode": 2, 
                            "errorText": "seloger",                                 
                            "tile_type": "person"
                        }
                    ]
                }
            ]
        },
        {
            "name": "Leaders",
            "displayOrder": 1,
            "groups": [{
                    "type": "static",
                    "displayOrder": 1,                      
                    "tiles": [{
                            "context": "event",
                            "dataStamp": 1535184247,
                            "eventId":123
                            "tile_type": "static"
                        }
                    ]
                }
            ]
        }

    ]
}]

在此响应中,如果平铺对象包含键errorCode,则必填字段必须是errorTexterrorCode键。与wise类似,如果平铺对象不包含任何“errorCode”或“errorText”键,则平铺项包含必填字段“dataStamp”。

为了验证上述条件,我定义了如下模式。但它不起作用。我的模式有什么问题。

 {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "views": {
                "$id": "views",
                "type": "array",
                "items": {
                    "$id": "views/items",
                    "type": "object",
                    "properties": {
                        "groups": {
                            "$id": "views/groups",
                            "type": "array",
                            "items": {
                                "$id": "views/groups/items",
                                "type": "object",
                                "properties": {
                                    "tiles": {
                                        "$id": "views/groups/tiles",
                                        "type": "array",
                                        "items": {
                                            "$id": "views/groups/tiles/items",
                                            "type": "object",
                                            "properties": {
                                                "dataStamp": {
                                                    "$id": "views/groups/tiles/dataStamp",
                                                    "type": "integer"
                                                },
                                                "tile_type": {
                                                    "$id": "views/groups/tiles/tile_type",
                                                    "type": "string"
                                                },
                                                "errorCode": {
                                                    "type": "integer",
                                                    "enum": [
                                                        2, 10
                                                    ]
                                                },
                                                "errorText": {
                                                    "type": "string",
                                                    "enum": [
                                                        "seloger", "france24"
                                                    ]
                                                }
                                            },
                                            "if": {
                                                "properties": {
                                                    "tile_type": {
                                                        "enum": ["person"]
                                                    },
                                                    "errorCode": {
                                                        "enum": [2, 10]
                                                    }

                                                },
                                                "required": ["errorCode", "errorText"]
                                            }

                                        }
                                    }
                                },
                                "required": [
                                    "type",
                                    "tiles"
                                ]
                            }
                        }
                    },
                    "required": [
                        "groups"
                    ]
                }
            }
        },
        "required": [
            "views"
        ]
    }
}

共1个答案

匿名用户

属性中的if语句缺少必需的

                                        "if": {
                                            "properties": {
                                                "tile_type": {
                                                    "enum": ["person"]
                                                },
                                                "errorCode": {
                                                    "enum": [2, 10]
                                                },
                                                "required": ["errorCode"]
                                            }
                                        },

如果没有所需的,则仅当设置了属性时,才会验证属性的值。所以原始的if模式将传递任何没有tile\u typeerrorCode的对象。

https://stackoverflow.com/a/51034071/329463可能会给你一些关于构建独家属性集群的灵感。

编辑:修改了完整模式

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "views": {
                "$id": "views",
                "type": "array",
                "items": {
                    "$id": "views/items",
                    "type": "object",
                    "properties": {
                        "groups": {
                            "$id": "views/groups",
                            "type": "array",
                            "items": {
                                "$id": "views/groups/items",
                                "type": "object",
                                "properties": {
                                    "tiles": {
                                        "$id": "views/groups/tiles",
                                        "type": "array",
                                        "items": {
                                            "$id": "views/groups/tiles/items",
                                            "type": "object",
                                            "properties": {                                                                                                                                             
                                                "dataStamp": {
                                                    "$id": "views/groups/tiles/dataStamp",
                                                    "type": "integer"
                                                },                                              
                                                "tile_type": {
                                                    "$id": "views/groups/tiles/tile_type",
                                                    "type": "string"
                                                },
                                                "errorCode": {
                                                    "type": "integer",
                                                    "enum": [
                                                        2, 10
                                                    ]
                                                },
                                                "errorText": {
                                                    "type": "string",
                                                    "enum": [
                                                        "seloger", "france24"
                                                    ]
                                                }
                                            },
                                            "if": {
                                                "properties": {
                                                    "tile_type": {
                                                        "enum": ["person"]
                                                    },
                                                    "errorCode": {
                                                        "enum": [2, 10]
                                                    }

                                                },
                                                "required":["errorCode"]
                                            },
                                            "then": {
                                                "required": ["errorCode", "errorText"]
                                            },
                                            "else": {
                                                "required": ["dataStamp"]
                                            }

                                        }
                                    }
                                },
                                "required": [
                                    "type",
                                    "tiles"
                                ]
                            }
                        }
                    },
                    "required": [
                        "groups"
                    ]
                }
            }
        },
        "required": [
            "views"
        ]
    }
}