Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 使用Postman进行架构验证时出现问题_Validation_Postman_Schema - Fatal编程技术网

Validation 使用Postman进行架构验证时出现问题

Validation 使用Postman进行架构验证时出现问题,validation,postman,schema,Validation,Postman,Schema,我的请求正文: [ { "postId": 1, "id": 1, "name": "name abc", "email": "Eliseo@gardner.biz", "body": "something" }, ... ] 我正在尝试验证它,如下所示: var schema = { "type": "array", "properties": { "postId": { "type": "integer"

我的请求正文:

[
  {
    "postId": 1,
    "id": 1,
    "name": "name abc",
    "email": "Eliseo@gardner.biz",
    "body": "something"
  },
...
]
我正在尝试验证它,如下所示:

var schema = {
  "type": "array",
  "properties": {
    "postId": {
      "type": "integer"
    },
    "id": {
      "type": "integer"
    },
     "name": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "pattern": "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"
    },
    "body": {
      "type": "string"
    }
  },
  "required": [
    "postId",
    "id",
    "name",
    "email",
    "body"
  ]
};

pm.test('Schemat jest poprawny', function() {
  pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});
即使我更改了例如字符串的id类型或无效的电子邮件模式,测试仍然正常


这段代码有什么问题?

我建议不要使用tv4进行模式验证,而是使用内置函数,就像它使用的那样

除此之外,您的模式看起来不正确,并且缺少针对
对象的验证,看起来它是针对
数组进行验证的

这可能会帮助您:

let schema = {
    "type": "array",
    "items": {
        "type": "object",
        "required": [
            "postId",
            "id",
            "name",
            "email",
            "body"
        ],
        "properties": {
            "postId": {
                "type": "integer"
            },
            "id": {
                "type": "integer"
            },
            "name": {
                "type": "string"
            },
            "email": {
                "type": "string",
                "pattern": "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"
            },
            "body": {
                "type": "string"
            }
        }
    }
}

pm.test("Schemat jest poprawny", () => {
    pm.response.to.have.jsonSchema(schema)
})

如果这对你有帮助,你能接受这个答案并投票吗。