简体   繁体   English

Postman 无法验证 JSON 架构中的所有对象

[英]Postman Trouble Validating All Objects in JSON Schema

It seems like it should be straight-forward enough, but I'm having problems fully validating JSON response data within Postman (Tiny Validator 4) when there is more than one object returned.看起来它应该足够直截了当,但是当返回多个 ZA8CFDE6331BD59EB2AC96F8911C4B666 时,我在完全验证 Postman(Tiny Validator 4)中的 JSON 响应数据时遇到问题。 When doing negative testing, I found it is only checking the first object instead of all the objects to ensure all data is returned.在做负测的时候,我发现它只检查第一个 object 而不是所有对象,以确保返回所有数据。 I want to ensure ALL data returned in ALL objects is of the correct type and is present as expected.我想确保所有对象中返回的所有数据都是正确的类型并且按预期存在。 I realize this should never be an issue, but want to verify for regression in my test environment.我意识到这不应该是一个问题,但想在我的测试环境中验证回归。

Response Example:响应示例:

[
    {
        "productID": 1,
        "product": "Desktop",
        "versionID": 123,
        "version": "Win10 x64"
    },
    {
        "productID": 2,
        "product": "Laptop",
        "versionID": 321,
        "version": "Win 10 x64"
    },
    {
        "productID": 3,
        "product": "Monitor",
        "versionID": 456,
        "version": "LCD Panel"
    }
];

Postman Schema and Object Setup: Postman 架构和 Object 设置:

var schema = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "productID": {
          "type": "integer"
        },
        "product": {
          "type": "string"
        },
        "versionID": {
          "type": "integer"
        },
        "version": {
          "type": "string"
        }
      },
      "required": [
        "productID",
        "product",
        "versionID",
        "version"
      ]
    }
  ]
}

var jsonData = pm.response.json();

var wrongDataType = [
    {
        "productID": 1,
        "product": "Desktop",
        "versionID": 123,
        "version": "Win10 x64"
    },
    {
        "productID": "2",       //data returned as a string instead of integer
        "product": "Laptop",
        "versionID": 321,
        "version": "Win 10 x64"
    }
];

var dataMissing = [
    {
        "productID": 1,
        "product": "Desktop",
        "versionID": 123,
        "version": "Win10 x64"
    },
    {
        "productID": "2",
        //"product": "Laptop",      //data not present in response
        "versionID": 321,
        "version": "Win 10 x64"
    }
];

Test Validation using Tiny Validator 4:使用 Tiny Validator 4 进行测试验证:

pm.test('Schema is valid', function() {
  pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});

pm.test('Wrong data type is not valid', function() {
  pm.expect(tv4.validate(wrongDataType, schema)).to.be.false;
});

pm.test('Data missing is not valid', function() {
  pm.expect(tv4.validate(dataMissing, schema)).to.be.false;
});

In the TV4 validation of the returned jsonData against the schema, it returns true because the API response is valid.在针对模式对返回的 jsonData 进行 TV4 验证时,它返回 true,因为 API 响应有效。 For the subsequent comparisons, comparing wrongDataType and dataMissing to the schema, the Postman tests show as failures due to the TV4 validation expecting an invalid comparison, but it actually matching.对于后续比较,将 wrongDataType 和 dataMissing 与模式进行比较,Postman 测试显示为失败,因为 TV4 验证预期无效比较,但实际上匹配。

If I put the wrong data type or missing data in the first object, it catches it, the validation fails, and the test passes.如果我在第一个 object 中输入了错误的数据类型或丢失的数据,它会捕获它,验证失败,并且测试通过。 However, if the incorrect data is put in any other returned object (as shown in the example), the test fails because it doesn't catch the errors in the second objects.但是,如果将不正确的数据放入任何其他返回的 object(如示例中所示)中,则测试将失败,因为它没有捕获第二个对象中的错误。

How do I get my tests to check all of the objects in a response and fails if any are incorrect or missing?如何让我的测试检查响应中的所有对象,如果有任何不正确或丢失则失败?

It took me a while, but you've specified items as an array.我花了一些时间,但您已将items指定为数组。 This means that each item is positionally specified.这意味着每个项目都是在位置上指定的。 Since you only have a single subschema, only the first item is verified.由于您只有一个子模式,因此仅验证了第一项。

If you just use the subschema, but not wrapped in an array, it should fix your issue.如果您只使用子模式,但没有包装在数组中,它应该可以解决您的问题。

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

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