Question

I've been trying to create a JSON Schema and have been using an online validation tool http://jsonschemalint.com/. I made a change to my JSON object that I expected to fail against my schema but it didn't - so I think I must have made a mistake somewhere. Can anyone explain to my why the following change doesn't cause a validation error?

Schema

{
"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"
            }
        }
    ]
}

]

Change

Find "targetID" : "ConfigType" under either the enabled or display dependency. Remove this line. This should then fail as these are both required fields according to the schema. However it doesn't seem to fail...

Was it helpful?

Solution

First I would recommend you move to draft 4 which have some improvements (required is provided in an array).

jsonschemalint is using draft 3. Afaik "items" constraint has not changed. You can provide a boolean value, an object or one array of objects.

In your case, you have provided one object-schema but incorrectly. "group" and "config" labels are not necessary. For instance, given the following json object in draft 3:

[{}]

This schema (similar as yours) validates the data:

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

And this makes the data invalid:

{
"type" : "array",
"minItems" : 1,
"items" : {
    "type" : "object",
    "properties" : {
        "one" : {
            "required" : true
        }
    }
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top