简体   繁体   English

使用 Postman 验证 JSON 架构

[英]Validate JSON Schema using Postman

I am trying to validate my response JSON Schema, but I am always getting failed test case,我正在尝试验证我的响应 JSON Schema,但我总是遇到失败的测试用例,

Returning JSON response is:返回 JSON 响应是:

[
    {
        "registrationOriginDateTime": "2021-11-27T21:11:11.000Z",
        "eventId": "qc0081902",
        "badgeId": "12367",
        "customerGuid": "322245671253455",
        "products": [],
        "status": "registered",
        "demographics": []
    }
]

I am using following approach for validating schema,我正在使用以下方法来验证架构,

var jsonSchema = {
"type": "array",
"items":{
type: "object",
properties:
{

        "registrationOriginDateTime": {"type":"string"},
        "eventId": {"type":"string"},
        "badgeId": {"type":"string"},
        "customerGuid": {"type":"string"},
        "products": {"type":"string"},
        "status": {"type":"string"},
        "demographics": {"type":"string"}

}
}
}

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

Yet it is failing.然而它正在失败。 Can someone help me on this?有人可以帮我吗?

Your schema for objects "products" and "demographics" are of type: array,the items within them are strings, see below:您的对象“产品”和“人口统计”的模式是类型:数组,其中的项目是字符串,见下文:

//set Response Body variable
var jsonData = pm.response.json();

//set Response Schema variable 
var schema = {
  "type": "array",
  "items": {
    "type": "object",
    "required": [],
    "properties": {
      "registrationOriginDateTime": {"type": "string"},
      "eventId": {"type": "string"},
      "badgeId": {"type": "string"},
      "customerGuid": {"type": "string"},
      "products": {"type": "array",
        "items": {"type": "string"}
      },
      "status": {"type": "string"},
      "demographics": {"type": "array",
        "items": {"type": "string"}
      }
    }
  }
};

//validate Response Schema against Response Body
pm.test('Response schema type nodes verification', function() {
  pm.expect(tv4.validate(jsonData, JSON.parse(schema), false, true), tv4.error).to.be.true;
});

To make the test cleaner, I would save the schema aas a value to global variables with key responseSchema , so the var schema declaration would look like the following:为了使测试更清晰,我会将模式 a 作为值保存到带有键responseSchema的全局变量中,因此 var 模式声明如下所示:

var schema = pm.globals.get("responseSchema");

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

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