2013-12-19 54 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