【发布时间】:2019-08-07 11:27:02
【问题描述】:
我正在尝试对 JSON 有效负载具有“步骤”数组且其内容可能只是预定义的一组选项之一的场景进行建模,例如:
{ "steps": ["s0"] }
or
{ "steps": ["s1"] }
or
{ "steps": ["s0", "s2"] }
如何在架构中对此进行建模?以下:
{
"$id": "https://example.com/person.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Payload",
"type": "object",
"properties": {
"steps": {
"type": "array",
"oneOf": [
["s0"],
["s1"],
["s0", "s2"]
]
}
}
}
失败,“读取架构时出现意外标记:StartArray.Path 'properties.steps.oneOf[0]'”
编辑
抱歉移动球门柱,我将我的问题简化为建议的解决方案适用于简化
版本但不是原版。更复杂的是,我需要 $ref 对象而不是 string 值...
示例输入:
{ "steps": [{"name": "S0"}] }
or
{ "steps": [{"name": "S1"}] }
or
{ "steps": [{"name": "S1"}, {"name": "S2"}] }
与预期不匹配的架构(遵循 @EylM 的建议)
{
"$id": "https://example.com/person.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Request",
"type": "object",
"properties": {
"steps": {
"type": "array",
"oneOf": [
{
"const": [
{"$ref": "#/definitions/s0"}
]
},
{
"const": [
{"$ref": "#/definitions/s1"}
]
},
{
"const": [
{"$ref": "#/definitions/s1"},
{"$ref": "#/definitions/s2"}
]
}
]
}
},
"required": [
"steps"
],
"definitions": {
"s0": {
"type": "object",
"properties": {"name": { "const": "S0" }}
},
"s1": {
"type": "object",
"properties": {"name": {"const": "S1" }}
},
"s2": {
"type": "object",
"properties": {"name": { "const": "S2" }}
}
}
}
使用此架构并输入 { "steps":[{"name": "s0"}] } 我得到
JSON 对来自 'oneOf' 的任何模式都有效
不管有什么价值,我都在使用https://www.jsonschemavalidator.net/ 进行实验。
【问题讨论】:
标签: json jsonschema