Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用postman和tv4针对具有多个元素的json数组验证jsonschema_Postman_Jsonschema_Tv4 - Fatal编程技术网

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

使用postman和tv4针对具有多个元素的json数组验证jsonschema,postman,jsonschema,tv4,Postman,Jsonschema,Tv4,下面是我的JSON模式 { "$schema": "http://json-schema.org/draft-04/schema#", "type": "array", "items": [ { "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "stri

下面是我的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”在模式中标记为required,但在json的第二项中缺少它。不过tv4验证还是通过了

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

观察结果是,如果station元素在任何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]

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

我认为类似的内容会对您起作用,并显示问题:

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

如何将上述示例中的一个Items元素标记为“必需”我不确定是否理解您的问题。您正确地使用了
required
。删除
项目中的
[
]
后,一切都应按预期进行。将验证库从tv4更改为agv对我很有效