2017-10-04 92 views
2

唯一鍵我具有以下JSON數據:JSON模式驗證:在JSON數組

[ 
    { 
    "unique1":{ 
    "id":3 
    } 
    }, 
    { 
    "unique2":{ 
    "id":4 
    } 
    } 
] 

每個陣列項具有與一個頂層唯一密鑰JSON對象。 當我嘗試爲它編寫驗證模式時,我只能驗證完整數組是唯一的,而不是每個數組中的頂級密鑰。

{ 
    "$schema": "http://json-schema.org/draft-06/schema#", 
    "type": "array", 
    "uniqueItems": true, 
    "items": { 
    "type": "object", 
    "patternProperties": { 
     "^.*$": { 
     } 
    } 
    } 
} 

繼JSON數據應無法驗證:

[ 
    { 
    "unique1":{ 
    "id":3 
    } 
    }, 
    { 
    "unique1":{ 
    "id":4 
    } 
    } 
] 

回答

0

沒有標準的JSON模式的關鍵字,允許表達此驗證要求。

Ajv(對於JavaScript)有一個自定義關鍵字「uniqueItemProperties」(在ajv-keywords包中),它可以滿足您的要求。

您可以提出該標準的下一版本。