2017-02-03 69 views
1

我試圖創建以下列方式爲JSON模式:JSON模式要求節點

{ 
    "$schema": "http://json-schema.org/schema#", 
    "title": "Layout", 
    "description": "The layout created by the user", 
    "type": "object", 
    "definitions": { 
    "stdAttribute": { 
     "type": "object", 
     "properties": { 
     "attributeValue": { 
      "type": "object" 
     }, 
     "attributeName": { 
      "type": "string" 
     } 
     } 
    }, 
    "stdItem": { 
     "type": "object", 
     "required" : ["stdAttributes"], 
     "properties": { 
     "stdType": { 
      "enum": [ 
      "CONTAINER", 
      "TEXT", 
      "TEXTAREA", 
      "BUTTON", 
      "LABEL", 
      "IMAGE", 
      "MARCIMAGE", 
      "DATA", 
      "SELECT", 
      "TABLE" 
      ] 
     }, 
     "stdAttributes": { 
      "type": "array", 
      "items": { 
      "$ref": "#/definitions/stdAttribute" 
      } 
     }, 
     "children": { 
      "type": "array", 
      "items": { 
      "$ref": "#/definitions/stdItem" 
      } 
     } 
     } 
    } 
    }, 
    "properties": { 
    "layoutItem": { 
     "$ref": "#/definitions/stdItem" 
    } 
    } 
} 

我確認以下JSON反對:

{ 
    "layoutItem": { 
    "stdItem": { 
     "stdType": "CONTAINER", 
     "stdAttributes": [], 
     "children": [] 
    } 
    } 
} 

問題是當我運行java驗證程序時出現錯誤,因爲我按照「stdItem」的要求指定了「stdAtrributes」節點,驗證程序無法對其進行鑑別。

我試圖在屬性中定義所需的數組,但模式被取消。

如果我把 「stdAttributes」 外 「stdItem」,它的工作原理。

有誰知道我該如何定義這個要求「stdItem」?

回答

0

您的模式中的問題是,您希望layoutItem的值根據模式#/definitions/stdItem有效。

但這種模式不符合,只要你想(查看數據)一個stdItem屬性來定義一個對象,它定義了帶屬性stdTypestdAttributeschildren對象,並要求物業stdAttributes存在。換句話說,它是下列數據的模式:

{ 
    "layoutItem": { 
    "stdType": "CONTAINER", 
    "stdAttributes": [], 
    "children": [] 
    } 
} 

爲您的數據的模式應該是:

{ 
    ... 
    "definitions": { 
    ... 
    "stdItem": { 
     "type": "object", 
     "required" : ["stdItem"], 
     "properties": { 
     "stdItem": { 
      "type": "object", 
      "required" : ["stdAttributes"], 
      "properties": { 
      "stdType": { ... }, 
      "stdAttributes": { ... }, 
      "children": { ... } 
      } 
     } 
     } 
    } 
    }, 
    ... 
} 
+0

它的工作原理!非常感謝你。 – Jeyvison