2016-12-12 129 views
4

我試圖驗證對象,其可具有任意的鍵,其值不是一個對象,看起來像: { "href": "some string" } JSON模式:使用anyOf,oneOf,allOf內additionalProperties

或含陣列對象的匹配上述。

這是我目前擁有並不起作用:

{ 
    "$schema": "http://json-schema.org/schema#", 
    "id": "https://turnstyle.io/schema/links.json", 
    "type": "object", 
    "additionalProperties": { 
     "oneOf": [ 
      { 
       "type": "object", 
       "required": "href" 
      }, 
      { 
       "type": "array", 
       "items": { 
        "type": "object", 
        "required": "href" 
       } 
      } 
     ] 
    } 
} 



Passing example: 
{ 
    "customer": { "href": "/customer/1" }, 
    "products": [ 
     { "href": "/product/1" }, 
     { "href": "/product/2" } 
    ] 
} 

Failing example: 
{ 
    "customer": { "wrongKey": "/customer/1" }, 
    "products": "aString" 
} 

是否有可能,如果是這樣什麼是正確的語法?

我的假設是,這將無法正常工作,因爲oneOf|anyOf|allOfoneOf|anyOf|allOf中的傳遞模式必須適用於所有屬於additionalProperties的密鑰。

+0

「我的假設是,這是行不通的,因爲在oneOf傳球模式(S)| anyOf | allOf additionalProperties必須適用於根據additionalProperties內的所有鑰匙。」 不,根據oneOf內的不同架構,不同的密鑰可以是有效的。在這種情況下anyOf也更有效率。 – esp

回答

4

「required」應該是v4中必需的屬性的數組。

或「必需」:true(或false)作爲v3中屬性的一部分。

試試這個:

{ 
    "$schema": "http://json-schema.org/schema#", 
    "id": "https://turnstyle.io/schema/links.json", 
    "type": "object", 
    "additionalProperties": { 
     "oneOf": [ 
      { 
       "type": "object", 
       "properties": { 
        "href": {"type": "string"} 
       }, 
       "required": ["href"] 
      }, 
      { 
       "type": "array", 
       "items": { 
        "type": "object", 
        "properties": { 
         "href": {"type": "string"} 
        }, 
        "required": ["href"] 
       } 
      } 
     ] 
    } 
} 
+0

該模式缺少「required」關鍵字。它會接受一個空對象作爲值或空對象數組。 –

+0

@AsyaKamsky你是對的。這並不完全清楚,如果這確實是一個公認的輸入,但似乎不是,所以我相應地更新了架構 – Pedro