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

Validate jsonschema against a json array having multiple elements using postman and tv4

下面是我的 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 验证。

我们真正需要的是,它应该无法通过验证,因为第二个项目中缺少站元素

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

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...仍然通过了验证

感谢任何帮助

items 关键字可以采用模式或模式数组,它具有不同的语义,具体取决于所使用的版本。

items 采用单个模式时,它描述了一个数组,其中数组中的所有项目都必须符合给定的模式。

{
  "type": "array".
  "items": { "type": "string" }
}

["a", "b", "c"]

items 采用模式数组时,它描述了一个元组,其中 items 中的每个模式都与实例数组中的相应项目进行比较。

{
  "type": "array",
  "items": [{ "type": "string" }, { "type": "integer" }, { "type": "boolean }]
}

["a", 1, false]

您感到困惑,因为您使用了错误的 items 形式。因为您使用了数组形式,所以只验证数组中的第一个元素。验证器忽略其余项目。

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

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 模块。