2014-11-05 38 views
0

我嘗試爲我的數據編寫json模式。這些數據看起來是這樣:Json-Schema:同一類型的多個元素

{ 
    "gold": [ 
     { 
      "id": "goldOne", 
      "name": "firstGold", 
      "title": "Gold 1 earned" 
     }, 
     { 
      "id": "goldTwo", 
      "name": "secondGold", 
      "title": "Gold 2 earned" 
     } 
    ], 
    "silver": [ 
     { 
      "id": "silberOne", 
      "name": "firstSilver", 
      "title": "Silver!" 
     } 
    ], 
    "bronze": [ 
     { 
      "id": "bronzeOne", 
      "name": "firstBronze", 
      "title": "Bronze!" 
     } 
    ] 
} 

我已經創建的模式爲「金」 - 陣列:

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "title" : "trophy descriptions", 
    "type": "object", 
    "properties": { 
     gold: { 
      "description": "gold trophies", 
      "type":"array", 
      "items": { 
       "type": "object", 
       "properties": { 
        "id": { 
         "type": "string", 
         "description": "unique identifier" 
        }, 
        "name": { 
         "type": "string", 
         "description": "label of trophy" 
        }, 
        "title": { 
         "type": "string", 
         "description": "description of trophy" 
        } 
       } 
      } 
     } 
    } 
} 

因爲「銀」和「銅」 -arrays包含完全相同的元素鍵入爲「黃金」我不知道是否我必須三次寫下相同的東西,或者我可以參考單個描述?

回答

2

是的,你可以定義並通過$ ref關鍵字參考架構:

{ 
    "$schema" : "http://json-schema.org/draft-04/schema#", 
    "title" : "trophy descriptions", 
    "type" : "object", 
    "properties" : { 
     "gold" : { 
      "$ref" : "#/definitions/medal" 
     }, 
     "silver" : { 
      "$ref" : "#/definitions/medal" 
     }, 
     "bronze" : { 
      "$ref" : "#/definitions/medal" 
     } 

    }, 

    "definitions" : { 
     "medal" : { 
      "type" : "array" 
      // and whatever you want here 
     } 
    } 
} 
相關問題