gpt4 book ai didi

jsonschema - Json 架构 : either declaring an array of one type OR another

转载 作者:行者123 更新时间:2023-12-02 15:09:53 26 4
gpt4 key购买 nike

我如何声明一个属性,它可以是一种类型的数组,即字符串,或另一种类型的数组,即整数?我四处搜索,我所能找到的只是一种声明混合类型数组的方法。

我尝试了以下架构定义:

{
"$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"
]
}

我应该认为无效。

最佳答案

您使用了错误的 items 关键字变体。有一种形式采用架构,另一种变体采用架构数组。

架构变体意味着所有元素都必须匹配架构。以下架构定义了一个数组,其中所有值都是字符串。

{
"items": { "type": "string" }
}

模式变体数组描述了一个元组。第一个模式验证第一项,第二个模式验证第二项,依此类推。以下模式验证一个数组,其中第一项是整数,第二项是字符串,第三项是 bool 值。

{
"items": [
{ "type": "integer" },
{ "type": "string" },
{ "type": "boolean" }
]
}

很少需要 items 的元组变体。您几乎总是想要另一个。

关于jsonschema - Json 架构 : either declaring an array of one type OR another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44756888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com