在 JSON 模式中定义枚举数组的正确方法
Correct way to define array of enums in JSON schema
我想用 JSON 模式数组来描述,它应该由零个或多个预定义值组成。为了简单起见,让我们使用这些可能的值:one
、two
和 three
。
正确的数组(应通过验证):
[]
["one", "one"]
["one", "three"]
不正确:
["four"]
现在,我知道应该使用"enum"
属性,但是我找不到相关资料放在哪里。
选项A(在"items"
下):
{
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
选项 B:
{
"type": "array",
"items": {
"type": "string"
},
"enum": ["one", "two", "three"]
}
选项A正确,满足您的要求。
{
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
根据json-schema
documentation,array
的枚举值必须包含在"items"
字段中:
{
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
如果您有一个 array
可以容纳例如不同类型的项目,那么您的架构应该如下所示:
{
"type": "array",
"items": [
{
"type": "string",
"enum": ["one", "two", "three"]
},
{
"type": "integer",
"enum": [1, 2, 3]
}
]
}
我想用 JSON 模式数组来描述,它应该由零个或多个预定义值组成。为了简单起见,让我们使用这些可能的值:one
、two
和 three
。
正确的数组(应通过验证):
[]
["one", "one"]
["one", "three"]
不正确:
["four"]
现在,我知道应该使用"enum"
属性,但是我找不到相关资料放在哪里。
选项A(在"items"
下):
{
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
选项 B:
{
"type": "array",
"items": {
"type": "string"
},
"enum": ["one", "two", "three"]
}
选项A正确,满足您的要求。
{
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
根据json-schema
documentation,array
的枚举值必须包含在"items"
字段中:
{
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
如果您有一个 array
可以容纳例如不同类型的项目,那么您的架构应该如下所示:
{
"type": "array",
"items": [
{
"type": "string",
"enum": ["one", "two", "three"]
},
{
"type": "integer",
"enum": [1, 2, 3]
}
]
}