gpt4 book ai didi

postman - 使用 postman 和 tv4 针对具有多个元素的 json 数组验证 jsonschema

转载 作者:行者123 更新时间:2023-12-02 02:56:38 24 4
gpt4 key购买 nike

下面是我的 JSON 架构

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"stations": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"serial_number": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"id",
"serial_number",
"name"
]
}
]
}
},
"required": [
"id",
"name",
"stations"
]
}
]
}

下面是要验证的json

[
{
"id": 1,
"name": "Test location",
"stations": [
{
"id": 1,
"serial_number": "TEST001",
"name": "TEST-STN!"
}
]

},
{
"id": 2,
"name": "Test location2"
}

]

此处元素“stations”在模式中被标记为必需,但在 json 的第二个项目中缺失。 tv4验证仍然通过。

我们真正需要的是,它应该验证失败,因为第二个 Item 中缺少 station 元素

观察结果是,如果任何 JSON 项中不存在站元素,则验证失败。但如果 station 元素存在在其中一个项目中,则验证通过

pm.test("Login Validation", function() { pm.expect(tv4.validate(pm.response.json(), pm.environment.get('schema.json'), true, true), tv4.error).to.be.true;});

我尝试了 tv4 选项“checkRecursive”,其值为 true 和 false...但它仍然通过了验证

感谢任何帮助

最佳答案

我认为这样的东西对你有用并显示问题:

let schema = {
"type": "array",
"items": {
"type": "object",
"required": [
"id",
"name",
"stations"
],
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"stations": {
"type": "array",
"items": {
"type": "object",
"required": [
"id",
"serial_number",
"name"
],
"properties": {
"id": {
"type": "integer"
},
"serial_number": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
}
}
}
}

pm.test("Check Schemat", () => {
pm.response.to.have.jsonSchema(schema)
})

我已经包含了 jsonSchema() Postman 函数,因为它使用 AJV 而不是较旧且当前未维护的 tv4 模块。

关于postman - 使用 postman 和 tv4 针对具有多个元素的 json 数组验证 jsonschema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60972062/

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