Json 架构:要么声明一种类型的数组,要么声明另一种类型的数组
Json Schema: either declaring an array of one type OR another
如何声明一个 属性 可以是一种类型的数组,即字符串,或另一种类型的数组,即整数?我四处搜索,我所能找到的只是一种声明混合类型数组的方法。
我尝试了以下模式定义:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"content": {
"oneOf": [
{
"type": "array",
"items": [
{
"type": "string"
}
]
},
{
"type": "array",
"items": [
{
"type": "integer"
}
]
}
]
}
},
"type": "object",
"properties": {
"content": {
"$ref": "#/definitions/content"
}
}
}
验证
{
"content": [
1, 2
]
}
和
{
"content": [
"a", "b"
]
}
但也验证
{
"content": [
1, "a"
]
}
我认为无效。
您的架构按顺序显示。我倾向于认为这将是您使用它来验证 [1, "a"]
变体的实现中的错误。您是否使用不同的实现对此进行了测试,或者考虑过对您在此处尝试过的实现提交错误?
您使用了 items
关键字的错误变体。有一种形式采用架构,另一种变体采用架构数组。
架构变体意味着所有元素都必须与架构匹配。以下架构定义了一个数组,其中所有值都是字符串。
{
"items": { "type": "string" }
}
模式变体数组描述了一个元组。第一个模式验证第一项,第二个模式验证第二项,依此类推。以下模式验证一个数组,其中第一项是整数,第二项是字符串,第三项是布尔值。
{
"items": [
{ "type": "integer" },
{ "type": "string" },
{ "type": "boolean" }
]
}
很少需要 items
的元组变体。你几乎总是想要另一个。
如何声明一个 属性 可以是一种类型的数组,即字符串,或另一种类型的数组,即整数?我四处搜索,我所能找到的只是一种声明混合类型数组的方法。
我尝试了以下模式定义:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"content": {
"oneOf": [
{
"type": "array",
"items": [
{
"type": "string"
}
]
},
{
"type": "array",
"items": [
{
"type": "integer"
}
]
}
]
}
},
"type": "object",
"properties": {
"content": {
"$ref": "#/definitions/content"
}
}
}
验证
{
"content": [
1, 2
]
}
和
{
"content": [
"a", "b"
]
}
但也验证
{
"content": [
1, "a"
]
}
我认为无效。
您的架构按顺序显示。我倾向于认为这将是您使用它来验证 [1, "a"]
变体的实现中的错误。您是否使用不同的实现对此进行了测试,或者考虑过对您在此处尝试过的实现提交错误?
您使用了 items
关键字的错误变体。有一种形式采用架构,另一种变体采用架构数组。
架构变体意味着所有元素都必须与架构匹配。以下架构定义了一个数组,其中所有值都是字符串。
{
"items": { "type": "string" }
}
模式变体数组描述了一个元组。第一个模式验证第一项,第二个模式验证第二项,依此类推。以下模式验证一个数组,其中第一项是整数,第二项是字符串,第三项是布尔值。
{
"items": [
{ "type": "integer" },
{ "type": "string" },
{ "type": "boolean" }
]
}
很少需要 items
的元组变体。你几乎总是想要另一个。