Json 模式 - 使用引用使用枚举
Json Schema - Using enumerations using reference
我正在尝试为我的用例构建一个 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": [..]
....
}
$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"] }
我正在尝试为我的用例构建一个 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": [..]
....
}
$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"] }