Postman 数组模式验证数组、模式、Postman

2023-09-07 11:04:44 作者:怪咖逗比

I have the problem with array json schema validation in postman.

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"
    },
 .
 .
 .
]
6种方式实现数组扁平化

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. The correct JSON schema would look like:

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. There is no type as long.