2016-08-31 23 views
3

我想爲我的用例構建一個JSON schema,其中我有一個單獨的文件中的字符串枚舉並希望從我的模式中引用該枚舉。我怎樣才能做到這一點。Json Schema - 使用參考枚舉

示例模式:

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "object", 
    "properties": { 
     "card": { 
      "type": "object", 
      "properties": { 
       "id": { 
        "type": "integer" 
       }, 
       "value": { 
        "type": "string", 
        "enum": {"$ref" : "reference to a file having list of enums"} 
        //I want to refer to a specific enum array (say value1's array) 
       } 
      } 
     } 
    }, 
    "required": [ 
     "card" 
    ] 
} 

枚舉文件是這樣的:

{ 
"value1": [..], 
"value2": [..] 
.... 
} 

回答

2

$ref只能用來參考的模式。所以,你可以做這樣的事情。

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "object", 
    "properties": { 
     "card": { 
      "type": "object", 
      "properties": { 
       "id": { "type": "integer" }, 
       "value": { "$ref" : "/schemas/valueEnum.json" } 
      } 
     } 
    }, 
    "required": ["card"] 
} 

/schemas/valueEnum.json

{ "enum": ["foo", "bar"] }