如何在 javascript 的邮递员中使用 ajv.addFormat()

How to use ajv.addFormat() in postman with javascript

我正在尝试验证邮递员中数据请求调用的格式。数据是 uuid 类型并且与 属性 id(如下所示)有关。

有人可以帮忙吗?

这是我的代码:


var jsonData = JSON.parse(responseBody);

var Ajv = require('ajv'),
ajv = new Ajv ({logger: console}),
schemaResponse = {
        "required": [
        "id",
        "ID",
    ],
        "type": "object",
        "properties": {
         "id": {
            "type": "string",
            "format": "uuidFormatCheck"
         }
};


Code wise it is like this:

var currentSchPmExpTest;

pm.test('Schema is valid', function() {
    //var data = pm.response.json();

    ajv.addFormat('uuidFormatCheck', /^[0-9e-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);

    currentSchPmExpTest = "schemaResponse variable";
    pm.expect(ajv.validate(schemaResponse, jsonData)).to.be.true;
});

body 响应是:

{
    "sfsid": "11c22abc-c11a-1df2-ba3a-123a456b78f1",
};

鉴于您提供的示例响应正文:

{
    "sfsid": "11c22abc-c11a-1df2-ba3a-123a456b78f1"
}

您可以像这样在 Postman 中使用 AJV:

let schemaResponse = {
  "type": "object",
  "required": [
    "sfsid"
  ],
  "properties": {
    "sfsid": {
      "type": "string",
      "pattern": "^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"
    }
  }
};

pm.test('Schema is valid', function() {
    pm.response.to.have.jsonSchema(schemaResponse)
});