2016-02-28 104 views
1

在我的模式我的電話對象的數組。每個對象都有一個「狀態」的屬性,它可以是以下三個值之一:「主」,「活動」,「不,在使用」。JSON模式對象性質約束

我想設置以下約束: 如果電話對象> 0的號碼,然後正好一個必須擁有的地位=「主」

這是可能的JSON模式?如果是這樣,怎麼樣?

回答

0

這個模式是非常接近你想要什麼。唯一的限制是,「主要」電話號碼需要是陣列中的第一個項目。

你也許能夠得到「主要」是一些創造性的運用not陣列中的任何地方。如果我弄明白了,我會更新答案。

{ 
    "type": "object", 
    "properties": { 
    "phoneNumbers": { 
     "type": "array", 
     "items": [{ "$ref": "#/definitions/primaryPhone" }], 
     "additionalItems": { "$ref": "#/definitions/additionalPhone" } 
    } 
    }, 
    "definitions": { 
    "phone": { 
     "type": "object", 
     "properties": { 
     "label": { "type": "string" }, 
     "number": { "type": "string" } 
     }, 
     "required": ["label", "number", "status"] 
    }, 
    "primaryPhone": { 
     "allOf": [{ "$ref": "#/definitions/phone" }], 
     "properties": { 
     "status": { "enum": ["Primary"] } 
     } 
    }, 
    "additionalPhone": { 
     "allOf": [{ "$ref": "#/definitions/phone" }], 
     "properties": { 
     "status": { "enum": ["Active", "Not-in-use"] } 
     } 
    } 
    } 
} 
+0

「'number」之前缺少逗號:{「type」:「string」}'。 –

+0

固定。謝謝@羅伯特 – Jason