JSON模式验证 - 为什么不强制执行?

时间:2013-12-19 15:10:15

标签: json jsonschema

我一直在尝试创建一个JSON Schema,并且一直在使用在线验证工具http://jsonschemalint.com/。我对我的JSON对象进行了更改,我预计会对我的架构失败,但它没有 - 所以我认为我肯定在某个地方犯了错误。谁能解释为什么以下更改不会导致验证错误?

模式

{
"title": "Control Configuration Array", 
"description": "An array of the configurations", 
"type": "array", 
"minItems": 1, 
"items": {
    "group": {
        "title": "Control Grouping", 
        "description": "Represents a logical grouping of controls", 
        "type": "object", 
        "properties": {
            "value": {
                "title": "Group Label", 
                "description": "The label to use for the group", 
                "type": "string"
            }, 
            "sortIndex": {
                "title": "Sort Index", 
                "description": "The order in which the groups appear", 
                "type": "number", 
                "minimum": 0
            }, 
            "cssClass": {
                "title": "Group CSS", 
                "description": "The CSS class to apply to the group label", 
                "type": "string"
            }, 
            "controls": {
                "title": "Controls", 
                "description": "The set of controls within this group", 
                "type": "array", 
                "minItems": 1, 
                "required": true, 
                "items": {
                    "config": {
                        "title": "Control Configuration", 
                        "description": "The main configuration object for a control and its associated dependencies", 
                        "type": "object", 
                        "properties": {
                            "id": {
                                "title": "Control ID", 
                                "description": "The identifier for the control set which will be used in dependencies", 
                                "type": "string", 
                                "required": true
                            }, 
                            "sortIndex": {
                                "title": "Sort Index", 
                                "description": "The order in which the controls appear", 
                                "type": "number", 
                                "minimum": 0
                            }, 
                            "label": {
                                "title": "Label", 
                                "description": "Describes the label for the control group", 
                                "type": "object", 
                                "properties": {
                                    "value": {
                                        "title": "Caption", 
                                        "description": "The caption to place in the label", 
                                        "type": "string"
                                    }, 
                                    "cssClass": {
                                        "title": "Label CSS Classes", 
                                        "description": "The CSS classes to apply to the label, separated with spaces", 
                                        "type": "string"
                                    }, 
                                    "tooltipText": {
                                        "title": "Tooltip", 
                                        "description": "The tooltip to apply to the label and control", 
                                        "type": "string"
                                    }
                                }
                            }, 
                            "control": {
                                "title": "Control", 
                                "description": "Describes the control for the control group", 
                                "type": "object", 
                                "required": true, 
                                "properties": {
                                    "type": {
                                        "title": "Control Type", 
                                        "description": "The type of control that should be displayed", 
                                        "type": "string", 
                                        "enum": [
                                            "text", 
                                            "radio", 
                                            "dropdown", 
                                            "checkbox", 
                                            "color", 
                                            "date", 
                                            "datetime", 
                                            "search", 
                                            "email", 
                                            "url", 
                                            "tel", 
                                            "number", 
                                            "range", 
                                            "month", 
                                            "week", 
                                            "time", 
                                            "datetime-local"
                                        ]
                                    }, 
                                    "options": {
                                        "title": "Avaliable Options", 
                                        "description": "The set of avaliable options for all selection controls (e.g. radio, dropdown)", 
                                        "type": "array"
                                    }, 
                                    "value": {
                                        "title": "The current value of the control", 
                                        "description": "This is the inital value or selected value of the control", 
                                        "type": "object", 
                                        "required": true
                                    }, 
                                    "cssClass": {
                                        "title": "Control CSS Classes", 
                                        "description": "The CSS classes to apply to the control, separated with spaces", 
                                        "type": "string"
                                    }
                                }
                            }, 
                            "dependencies": {
                                "title": "Dependencies", 
                                "description": "Describes the dependencies between this and other controls", 
                                "type": "object", 
                                "properties": {
                                    "enabled": {
                                        "title": "Enabled", 
                                        "description": "The properties to determine if the control should be enabled or not", 
                                        "type": "object", 
                                        "properties": {
                                            "targetID": {
                                                "title": "Enabled Target ID", 
                                                "description": "The ID of the target control, whose value must match one of the target values for this control to be enabled", 
                                                "type": "string", 
                                                "required": true
                                            }, 
                                            "targetValues": {
                                                "title": "Enabled target values", 
                                                "description": "The set of values which if selected in the target control will cause this control to be enabled", 
                                                "type": "array", 
                                                "required": true
                                            }
                                        }
                                    }, 
                                    "display": {
                                        "title": "Display", 
                                        "description": "The properties to determine if the control should be displayed or not", 
                                        "type": "object", 
                                        "properties": {
                                            "targetID": {
                                                "title": "Display Target ID", 
                                                "description": "The ID of the target control, whose value must match one of the target values for this control to be displayed", 
                                                "type": "string", 
                                                "required": true
                                            }, 
                                            "targetValues": {
                                                "title": "Display target values", 
                                                "description": "The set of values which if selected in the target control will cause this control to be displayed", 
                                                "type": "array", 
                                                "required": true
                                            }
                                        }
                                    }
                                }
                            }, 
                            "validation": {
                                "title": "Validation", 
                                "description": "Describes the validation of the control value", 
                                "type": "object", 
                                "properties": {
                                    "required": {
                                        "title": "Required", 
                                        "description": "Whether the field is required", 
                                        "type": "boolean"
                                    }, 
                                    "min": {
                                        "title": "Minimum", 
                                        "description": "The minimum value that the control is allowed", 
                                        "type": "number"
                                    }, 
                                    "max": {
                                        "title": "Maximum", 
                                        "description": "The maximum value that the control is allowed", 
                                        "type": "number"
                                    }, 
                                    "minLength": {
                                        "title": "Minimum Length", 
                                        "description": "The minimum length that the control is allowed", 
                                        "type": "integer"
                                    }, 
                                    "maxLength": {
                                        "title": "Maximum Length", 
                                        "description": "The maximum length that the control is allowed", 
                                        "type": "integer"
                                    }, 
                                    "pattern": {
                                        "title": "Regex Pattern", 
                                        "description": "A regex pattern to use for validation", 
                                        "type": "string"
                                    }, 
                                    "step": {
                                        "title": "Increment Step", 
                                        "description": "An increment check that must be met - generally combine with min/max", 
                                        "type": "number"
                                    }, 
                                    "email": {
                                        "title": "Email", 
                                        "description": "Whether the field must be an email address", 
                                        "type": "boolean"
                                    }, 
                                    "equal": {
                                        "title": "Equals", 
                                        "description": "Ensure the field equals the other object", 
                                        "type": "object"
                                    }, 
                                    "notEqual": {
                                        "title": "Not Equals", 
                                        "description": "Ensure the field does not equal the other object", 
                                        "type": "object"
                                    }, 
                                    "date": {
                                        "title": "Date", 
                                        "description": "Whether the field must be a date", 
                                        "type": "boolean"
                                    }, 
                                    "dateISO": {
                                        "title": "Date ISO", 
                                        "description": "Whether the field must be an ISO date", 
                                        "type": "boolean"
                                    }, 
                                    "number": {
                                        "title": "Number", 
                                        "description": "Whether the field must be a number", 
                                        "type": "boolean"
                                    }, 
                                    "digit": {
                                        "title": "Digit", 
                                        "description": "Whether the field must be a digit", 
                                        "type": "boolean"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

}

JSON

[
{
    "value": "Group1", 
    "cssClass": "red", 
    "sortIndex": 1, 
    "controls": [
        {
            "id": "ConfigType", 
            "sortIndex": 1, 
            "label": {
                "value": "Configuration Type", 
                "cssClass": "label", 
                "tooltipText": "Configuration Type Tooltip"
            }, 
            "control": {
                "type": "radio", 
                "options": [
                    "Single Deck", 
                    "Level"
                ], 
                "value": "Single Deck", 
                "cssClass": "control"
            }
        }, 
        {
            "id": "AppType", 
            "sortIndex": 2, 
            "label": {
                "value": "Application Type", 
                "cssClass": "label", 
                "tooltipText": "Application Type Tooltip"
            }, 
            "control": {
                "type": "dropdown", 
                "options": [
                    "Other", 
                    "Other2"
                ], 
                "value": "Other", 
                "cssClass": "red"
            }, 
            "dependencies": {
                "enabled": {
                    "targetID": "ConfigType", 
                    "targetValues": [
                        "Level"
                    ]
                }, 
                "display": {
                    "targetID": "ConfigType", 
                    "targetValues": [
                        "Level"
                    ]
                }
            }
        }, 
        {
            "id": "textType", 
            "label": {
                "value": "Text Type", 
                "cssClass": "label", 
                "tooltipText": "text Type Tooltip"
            }
        }
    ]
}

更改

在启用或显示依赖项下找到“targetID”:“ConfigType”。删除此行。这应该失败,因为这些都是根据模式的必填字段。但它似乎没有失败......

1 个答案:

答案 0 :(得分:1)

首先,我建议你转到草案4,它有一些改进(在数组中提供了必需的。)

jsonschemalint正在使用草案3. Afaik“items”约束没有改变。您可以提供布尔值,对象或一个对象数组。

在您的情况下,您提供了一个对象架构但却不正确。 “group”和“config”标签不是必需的。例如,给定草案3中的以下json对象:

[{}]

此架构(与您的架构类似)验证数据:

{
    "type" : "array",
"minItems" : 1,
"items" : {
    "unnecesaryLabel" : {
        "type" : "object",
        "properties" : {
            "one" : {
                "required" : true
            }
        }
    }
}
}

这会使数据无效:

{
"type" : "array",
"minItems" : 1,
"items" : {
    "type" : "object",
    "properties" : {
        "one" : {
            "required" : true
        }
    }
}
}
相关问题