JSON 架构验证数组的头部和尾部
JSON schema validate head and tail of and array
嗨,我有一个简单的 JSON 数组
其中第一项始终是字符串
和 2..N 项可以是布尔值或整数。例如
[ "string", 1, 1, true, 1] // valid
[ "string", 1,1,"string" ] // invalid
[ "string", 1,1,1,1,1,1] // valid
我试图想出一个 json 模式来验证这一点,但是
不幸的是,以上所有内容均有效。不确定是否可以在 json shema 中验证这一点,即头部和尾部?此列表(数组)可以包含任意数量的项目。
我的尝试:
{
"type" : "array",
"items" : [
{ "$ref": "#/definitions/head" },
{ "$ref": "#/definitions/tail" }
],
"definitions": {
"head" : {
"type": "string"
},
"tail": { "anyOf" : [
{ "type" : "number" },
{ "type" : "boolean" }
]}
}
}
您可以使用 items
的数组形式结合 additionalItems
来完成此操作。 items
描述头部,additionalProperties
描述尾部。
{
"type": "array",
"items": [{ "type": "string" }],
"additionalItems": { "type": ["number", "boolean"] }
}
2020-12新草案中,关键字发生了变化,但您可以做同样的事情。
{
"type": "array",
"prefixItems": [{ "type": "string" }],
"items": { "type": ["number", "boolean"] }
}
嗨,我有一个简单的 JSON 数组 其中第一项始终是字符串 和 2..N 项可以是布尔值或整数。例如
[ "string", 1, 1, true, 1] // valid
[ "string", 1,1,"string" ] // invalid
[ "string", 1,1,1,1,1,1] // valid
我试图想出一个 json 模式来验证这一点,但是 不幸的是,以上所有内容均有效。不确定是否可以在 json shema 中验证这一点,即头部和尾部?此列表(数组)可以包含任意数量的项目。 我的尝试:
{
"type" : "array",
"items" : [
{ "$ref": "#/definitions/head" },
{ "$ref": "#/definitions/tail" }
],
"definitions": {
"head" : {
"type": "string"
},
"tail": { "anyOf" : [
{ "type" : "number" },
{ "type" : "boolean" }
]}
}
}
您可以使用 items
的数组形式结合 additionalItems
来完成此操作。 items
描述头部,additionalProperties
描述尾部。
{
"type": "array",
"items": [{ "type": "string" }],
"additionalItems": { "type": ["number", "boolean"] }
}
2020-12新草案中,关键字发生了变化,但您可以做同样的事情。
{
"type": "array",
"prefixItems": [{ "type": "string" }],
"items": { "type": ["number", "boolean"] }
}