在 JSON 模式中定义枚举数组的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30924271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way to define array of enums in JSON schema
提问by senasi
I want to describe with JSON schema array, which should consist of zero or more predefined values. To make it simple, let's have these possible values: one, twoand three.
我想用 JSON 模式数组来描述,它应该由零个或多个预定义值组成。为了简单起见,让我们有这些可能的值:one,two和three。
Correct arrays (should pass validation):
正确的数组(应该通过验证):
[]
["one", "one"]
["one", "three"]
Incorrect:
不正确:
["four"]
Now, I know the "enum"property should be used, but I can't find relevant information where to put it.
现在,我知道"enum"应该使用该属性,但找不到相关信息将其放在哪里。
Option A (under "items"):
选项A(下"items"):
{
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
Option B:
选项 B:
{
"type": "array",
"items": {
"type": "string"
},
"enum": ["one", "two", "three"]
}
回答by jruizaranguren
Option A is correct and satisfy your requirements.
选项A是正确的,满足你的要求。
{
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
回答by Giorgos Myrianthous
According to the json-schemadocumentation, the enumerated values of an arraymust be included in the "items"field:
根据json-schema文档,字段中array必须包含an 的枚举值"items":
{
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
If you have an arraythat can hold e.g. items of different type, then your schema should look like the one below:
如果您有一个array可以容纳不同类型的项目,那么您的架构应该如下所示:
{
"type": "array",
"items": [
{
"type": "string",
"enum": ["one", "two", "three"]
},
{
"type": "integer",
"enum": [1, 2, 3]
}
]
}

