gpt4 book ai didi

arrays - 对象数组的 Json 架构未验证

转载 作者:行者123 更新时间:2023-12-02 20:57:46 26 4
gpt4 key购买 nike

我有一个 json 响应的架构

{
"title": "Products",
"description": "schema for products",
"type": "array",
"properties": {
"id": {
"description": "id of a product",
"type": "integer"
},
"name": {
"description": "name of the product",
"type": "string"
},
"created_at": {
"description": "record created_at",
"type": "string",
"format": "date-time"
},
"updated_at": {
"description": "record updated_at",
"type": "string",
"format": "date-time"
}
},
"required": ["id", "name"]
}

我想将此模式与此 json 相匹配

[{
"id": 1,
"name": "Cricket Ball"
}, {
"id": 2,
"name": "Soccer Ball"
}, {
"id": 3,
"name": "football ball"
}, {
"id": 4,
"name": "Basketball ball"
}, {
"id": 5,
"name": "Table Tennis ball"
}, {
"id": 6,
"name": "Tennis ball"
}]

此架构与响应匹配,但也与必填字段为 this 的架构匹配

"required": ["ids", "names"]

我认为架构是针对数组进行验证的,而数组中的对象未经过验证。

最佳答案

按照您现在设置的方式,您的 properties 键引用数组本身,而不是每个项目,并且被忽略(因为数组没有属性,它们只有项目)。您需要使用 items 键来验证数组中的每个项目,如下所示:

{
"title": "Products",
"description": "schema for products",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"description": "id of a product",
"type": "integer"
},
"name": {
"description": "name of the product",
"type": "string"
},
"created_at": {
"description": "record created_at",
"type": "string",
"format": "date-time"
},
"updated_at": {
"description": "record updated_at",
"type": "string",
"format": "date-time"
}
},
"required": ["id", "name"]
}
}

关于arrays - 对象数组的 Json 架构未验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39633226/

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