2017-10-12 28 views
0

草案4我想創建模式,讓我添加多個地址時,符合下列條件:JSON模式與無序排列

  • 1)通訊地址必須是有
  • 2)居住地址必須在那裏
  • 3)其他類型的地址可能是有
  • 4)他們可以以任何順序

出現這是可能的用JSON模式解決?我在草案6中聽說過「包含」。但由於我們正在使用支持草案4的Altova XML SPY(2018)創建模式,因此我想知道如何在草案4中解決此問題。您是否知道草稿6的優秀編輯器?

我讀JSON schema to enforce array contents,但在那裏找不到答案。我還讀了https://gist.github.com/anonymous/7359001,它解釋瞭如何在草案4中包含「包含」,但無法將其應用於我的案例。

所以,如果你能指導我,如何將它用於下面的模式(滿足我的需求除了數字4),我會非常感謝。

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "description": "Comment describing your JSON Schema", 
    "type": "object", 
    "properties": { 
     "addresses": { 
      "type": "array", 
      "items": [ 
       { 
        "type": "object", 
        "properties": { 
         "type": { 
          "type": "string", 
          "enum": [ 
           "Corespondence" 
          ] 
         }, 
         "otherData": {} 
        }, 
        "required": [ 
         "type", 
         "otherData" 
        ] 
       }, 
       { 
        "type": "object", 
        "properties": { 
         "type": { 
          "type": "string", 
          "enum": [ 
           "Residence" 
          ] 
         }, 
         "otherData": {} 
        }, 
        "required": [ 
         "type", 
         "otherData" 
        ] 
       } 
      ], 
      "additionalItems": { 
       "$ref": "#/definitions/Address" 
      } 
     } 
    }, 
    "definitions": { 
     "Address": { 
      "type": "object", 
      "properties": { 
       "type": { 
        "type": "string", 
        "enum": [ 
         "Corespondence", 
         "Residence", 
         "Other" 
        ] 
       }, 
       "otherData": {} 
      }, 
      "required": [ 
       "type", 
       "otherData" 
      ] 
     } 
    } 
} 

回答

0

draft-06具有「contains」關鍵字,允許測試與數組中模式匹配的項的存在。

另一方面,不是異構陣列可以使用在不同的性質確定差異地址類型的地圖,讓您的數據可能是:

{ 
    addresses: { 
    correspondence: {/*...*/}, 
    residence: {/*...*/} 
    // etc... 
    } 
} 
+0

能否請您概述瞭如何與地圖做呢?基本的JSON結構就足夠了....或者鏈接到一些文檔...謝謝 –

+0

加入回答 – esp

+0

啊,好的。現在我明白你的意思了。不幸的是,數據結構已經定義 - 如上所述(地址類型是Address對象內的一個屬性)。那麼,你認爲我的要求不能在沒有使用'包含'的情況下實現嗎? –