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)
})
如果这对你有帮助,你能接受这个答案并投票吗。