在 json 模式中,我可以简单地使用“enum”和可用代码列表定义代码列表,例如:
{
"type": "object",
"properties": {
"group": {
"type":"string",
"$ref": "#/definitions/Group"
}
},
"definitions": {
"Group": {
"enum": ["A","B"]
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下有效负载将是有效的:
{
"group": "B"
}
Run Code Online (Sandbox Code Playgroud)
但是,我尝试向用户提供架构中的描述,其中“A”=“A 组”,“B”=“B 组”。就像是:
{
"type": "object",
"properties": {
"group": {
"type":"string",
"$ref": "#/definitions/Group"
}
},
"definitions": {
"Group": {
"enum": [
{"code":"A",
"description": "Group A"
},
{"code":"B",
"description": "Group B"
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我不想改变有效负载的结构(不需要“描述”字段)描述更多的是用于用户可以参考的文档目的。这里有可以使用的好做法吗?
谢谢
您可以使用anyOf
withconst
来代替enum
. 然后您可以使用title
或description
作为文档。
{
"anyOf": [
{
"const": "A",
"title": "Group A"
},
{
"const": "B",
"title": "Group B"
}
]
}
Run Code Online (Sandbox Code Playgroud)