gpt4 book ai didi

嵌套对象中的 JSON 架构 v4 "required"

转载 作者:行者123 更新时间:2023-12-03 02:55:28 25 4
gpt4 key购买 nike

我尝试过搜索,但我不太确定如何用语言表达!令人困惑的地方是,当存在具有相同名称的嵌套键值时,“必需”在 JSON 模式 v4 中如何工作。

例如,此架构:

{
"Root": {
"type": ["array", "null"],
"items": {
"type": "object",
"properties": {
"LevelOne": {
"required": ["id", "name", "LevelOneRepeat"],
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"LevelOneRepeat": {
"type": ["array", "null"],
"items": {
"type": "object",
"properties": {
"required": ["id", "name"],
"id": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
}
}
}
}
}
}

在 LevelOne 中,我需要“id”、“name”和“LevelOneRepeat”。但是,在 LevelOneRepeat 内部,我还需要“id”和“name”。

required 是否只检查同一级别的元素,还是也检查所有子元素?如果所需的键值(相同名称)已在上面的级别中列出,我是否需要在 LevelOneRepeat 中包含所需的内容?

我用我的模式测试了我的 JSON,但我可能在某个地方弄乱了我的代码,因为不需要的东西已经不再工作了。

最佳答案

您的架构存在一些问题,这可能导致您对 required 的工作原理感到困惑。

这是更正后的架构。

{
"type": ["array", "null"],
"items": {
"type": "object",
"properties": {
"LevelOne": {
"type": "object",
"properties": {
"id": { "type": "string" },
"name": { "type": "string" },
"LevelOneRepeat": {
"type": ["array", "null"],
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"name": { "type": "string" }
},
"required": ["id", "name"]
}
}
},
"required": ["id", "name", "LevelOneRepeat"],
}
}
}
}

第一个问题是定义嵌套对象的方式。每个属性的值必须是一个架构。了解我如何更改 LevelOne 定义,以了解如何正确定义嵌套对象。

第二个问题是 required 关键字放在错误的位置。它应该与 properties 关键字处于同一级别,而不是嵌套在 properties 对象中。这就是为什么您的required 约束不起作用。了解我如何更改 LevelOneLevelOneRepeat 定义。

一旦解决了这些问题,希望您应该更清楚 required 关键字仅适用于定义它的架构。它不适用于任何父架构或子架构。

关于嵌套对象中的 JSON 架构 v4 "required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31664705/

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