2016-11-18 21 views
1

我JSON模式是這樣的:JSON模式檢查所有項目枚舉存在於對象的數組

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "array", 
    "items": { 
    "type": "object", 
    "properties": { 
     "country": { 
     "type": "string", 
     "maxLength": 2, 
     "enum": ["aa", "bb"] 
     } 
    }, 
    "required": [ 
     "country" 
    ] 
    } 
} 

和JSON格式如下:

[ 
    {"country": "aa"}, 
] 

我想架構檢查是否JSON文件包含enum中列出的所有國家或地區:

[ 
    {"country": "aa"}, 
    {"country": "bb"}, 
] 

是否有可能?

回答

1

你可以用V5/6做它包含關鍵字:

{ 
    "allOf": [ 
    { "contains": { "properties": { "country": { "constant": "aa" } } } }, 
    { "contains": { "properties": { "country": { "constant": "bb" } } } } 
    ] 
} 

"constant": "aa"是另一個V5/6的關鍵字,一樣"enum": ["aa"]。 目前Ajv支持這些關鍵字(有點自我推銷)。

+0

感謝您澄清這些事情) – xdrew

1

對於那些誰不能使用@esp方便語法,這裏是一箇舊式的解決方案:

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "array", 
    "items": { 
     "type": "object", 
     "properties": { 
      "country": { 
       "type": "string", 
       "maxLength": 2, 
       "enum": ["aa", "bb"] 
      } 
     }, 
     "required": [ 
      "country" 
     ] 
    }, 
    "allOf": [ 
     {"not": {"items": {"not": {"properties": {"country": {"enum": ["aa"]}}}}}}, 
     {"not": {"items": {"not": {"properties": {"country": {"enum": ["bb"]}}}}}} 
    ] 
}