gpt4 book ai didi

json - 无法验证架构并正确使用 additionalProperties

转载 作者:行者123 更新时间:2023-12-01 23:31:23 25 4
gpt4 key购买 nike

我正在尝试验证我的 JSON 架构并使用 additionalProperties: false 来确认没有其他属性。我的 responseBody 看起来像这样:

[
{
"id": 1234567890987654,
"email": "eemail@domain.com",
"civility": 0,
"firstname": "john",
"lastname": "do",
"function": null,
"phone": null,
"cellphone": null,
"role": 1,
"passwordws": "jdnfjnshn55fff5g8"
},
{
...}
]

在 postman 测试中,我添加了这个

var schema = {
"type": "array",
"properties": {
"id": {"type":"number"},
"email": {"type":"string"},
"civility": {"type":"number"},
"firstname": {"type":"string"},
"lastname": {"type":"string"},
"function": {"type":"string"},
"cellphone": {"type":"string"},
"role": {"type":"number"},
"passwordws": {"type":"string"},
},
"additionalProperties": false,
"required": ["id", "email", "civility", "role", "passwordws"]
};

var data = JSON.parse(responseBody);
var result = tv4.validateResult(data, schema);
tests["Valid schema"] = result.valid;

测试应该返回失败,因为我从架构中删除了“电话”属性,但测试仍然运行有效...我试图将架构更改为 {type:array, properties: {type: object, properties {list of properties}additionalProperties: false}} 但测试仍然返回 PASS 而不是 FAIL ...有什么想法吗?

最佳答案

你的响应是一个对象数组,我看到了:

  • 数组对象未定义

  • 用“数字”而不是“整数”定义类型 id

试试这个:

var schema = {
"type":"array",
"items": { "$ref": "#/definitions/MyObject" }

"definitions" : {
"MyObject" : {
"type":"object",
"required" : ["id", "email", "civility", "role", "passwordws"],
"properties": {
"id": {"type":"integer"},
"email": {"type":"string"},
"civility": {"type":"integer"},
"firstname": {"type":"string"},
"lastname": {"type":"string"},
"function": {"type":"string"},
"phone": {"type":"string"},
"cellphone": {"type":"string"},
"role": {"type":"integer"},
"passwordws": {"type":"string"}
},
"additionalProperties": false,
},
},
};

关于json - 无法验证架构并正确使用 additionalProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34286679/

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