简体   繁体   English

Postman 数组架构验证

[英]Postman array schema validation

I have the problem with array json schema validation in postman.我在邮递员中遇到数组 json 模式验证的问题。

var schema = {
    "type": "array",
    "items": [{
         "id": {
            "type":"long"
             },
         "name": {
             "type":"string"
             },
         "email": {
             "type":"string"
            }
    }]
};


pm.test('Response schema type nodes verification', function() {
  pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;
});

And the response body is:响应主体是:

[
    {
        "id": 1,
        "name": "test1",
        "email": "a@a.com"
    },
    {
        "id": 2,
        "name": "test2",
        "email": "a@a.com"
    },
 .
 .
 .
]

I have always passed result.我一直通过结果。 Also I tried with removed [] .我也试过删除[]

Where is the problem?问题出在哪儿?

The schema used in question is incorrect, you need to define the type of item in array as object .有问题的架构不正确,您需要将数组中的项目类型定义为object The correct JSON schema would look like:正确的 JSON 架构如下所示:

var schema = {
    "type": "array",
    "items": [{
        type: "object",
        properties:{
         "id": {
            "type":"integer"
             },
         "name": {
             "type":"string"
             },
         "email": {
             "type":"string"
            }
        }
    }]
};


pm.test('Response schema type nodes verification', function() {
  pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;
});

Please note there are only 2 numeric types in JSON Schema: integer and number .请注意,JSON Schema 中只有 2 种数字类型: integernumber There is no type as long .没有像long那样的类型。

You could also use Ajv , this is now included with the Postman native apps and the project is actively maintained:您也可以使用Ajv ,它现在包含在 Postman 本机应用程序中,并且该项目得到积极维护:

var Ajv = require("ajv"),
    ajv = new Ajv({logger: console}),
    schema = {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "id": { "type": "integer" },
                "name": { "type": "string" },
                "email": { "type": "string" }
            }  
        }
    };


pm.test("Schema is valid", function() {
        pm.expect(ajv.validate(schema, pm.response.json())).to.be.true;
});

Hi you should first parse the schema to json, else it will be considered as empty data in some cases.嗨,您应该首先将架构解析为 json,否则在某些情况下它将被视为空数据。

The correct code is as below:正确的代码如下:

let jsonData = JSON.parse(responseBody);

schema = JSON.parse(schema); 

Now you have to pass recursive validation and nondefined property check to the tv4.validation function.现在您必须将递归验证和非定义属性检查传递给 tv4.validation 函数。

pm.test('Response schema type nodes verification', function() {
  pm.expect(tv4.validate(pm.response.json(), schema, true, true)).to.be.true;
});

tv4.validate(pm.response.json(), schema, true, true) tv4.validate(pm.response.json(), 模式, 真, 真)

will check the json data recursively and if any new property is present in the resonse data, it fail the validation.将递归检查 json 数据,如果响应数据中存在任何新属性,则验证失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM