2013-12-19 77 views
0

我一直在试图创建一个JSON Schema并且使用在线验证工具http://jsonschemalint.com/。我对我的JSON对象做了一些改变,我预计会对我的模式失败,但它没有 - 所以我认为我一定在某个地方犯了错误。任何人都可以解释为什么以下更改不会导致验证错误?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" 
      } 
     } 
    ] 
} 

]

变化

在enabled或display依赖项下找到“targetID”:“ConfigType”。删除这一行。这应该会失败,因为这些都是根据模式的必需字段。然而,它似乎并没有失败...

回答

1

首先,我会建议你转移到草案4有一些改进(需要提供一个数组)。

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

就你而言,你提供了一个对象模式,但不正确。 “组”和“配置”标签是没有必要的。例如,给出下面的JSON对象在草案3:

[{}] 

此架构(如您的类似)对数据进行验证:

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

,这使得数据无效:

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

所以,如果我理解正确 - 在你的例子中向unccessaryLabel添加必需的true将不会对v3执行任何操作,但会在v4中解决我的问题? – Ian

+0

不,AFAIK项目属性行为在v3,v4中不会更改,因此您需要删除不必要的标签。我只是建议你转到v4以便使用最新的标准和库。您可能会发现v4中的改进功能可让您更轻松地表达约束条件。 – jruizaranguren

+0

在我当前的模式中,不需要额外的标签,因为该组具有多个使用的属性,这些属性应用于模式下方数组中的所有控件。 – Ian