gpt4 book ai didi

对象数组的 JSON 模式定义

转载 作者:行者123 更新时间:2023-12-02 17:45:49 31 4
gpt4 key购买 nike

我见过这个other question但并不完全相同,我觉得我的问题更简单,但就是不起作用。

我的数据如下所示:

[
{ "loc": "a value 1", "toll" : null, "message" : "message is sometimes null"},
{ "loc": "a value 2", "toll" : "toll is sometimes null", "message" : null}
]

我想使用AJV对于 Node.js 项目中的 JSON 验证,我尝试了几种模式来尝试描述我的数据,但我总是收到此错误:

[ { keyword: 'type',
dataPath: '',
schemaPath: '#/type',
params: { type: 'array' },
message: 'should be array' } ]

我尝试过的架构如下所示:

{
"type": "array",
"items": {
"type": "object",
"properties": {
"loc": {
"type": "string"
},
"toll": {
"type": "string"
},
"message": {
"type": "string"
}
},
"required": [
"loc"
]
}
}

我还尝试使用 this online tool 生成架构但这也不起作用,为了验证应该输出正确的结果,我尝试根据 jsonschemavalidator.net 验证该输出,但这也给了我一个类似的错误:

Found 1 error(s)
Message:
Invalid type. Expected Array but got Object.
Schema path:
#/type

最佳答案

您已正确定义架构,但它与您所说的正在验证的数据不匹配。如果更改属性名称以匹配架构,仍然会遇到一个问题。如果您想让“toll”和“message”为空,可以执行以下操作。

{
"type": "array",
"items": {
"type": "object",
"properties": {
"loc": {
"type": "string"
},
"toll": {
"type": ["string", "null"]
},
"message": {
"type": ["string", "null"]
}
},
"required": [
"loc"
]
}
}

但是,这与您收到的错误消息无关。该消息意味着您正在验证的数据不是数组。您发布的示例数据不应导致此错误。您是否正在对问题中发布的数据以外的某些数据运行验证器?

关于对象数组的 JSON 模式定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36757949/

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