[英]Postman Schema Validation using TV4
I'm having trouble validating a schema in Postman using tv4 inside the tests tab - it is always returning a true test, no matter what I feed it. 我在使用“测试”标签中的tv4验证Postman中的架构时遇到了麻烦-无论我提供什么,它总是返回真实的测试。 I am at a complete loss and could really use a hand - here is my example JSON Response, and my tests:
我完全不知所措,真的可以动手了-这是我的示例JSON响应和测试:
I've tried a ton of variations from every Stack Overflow/tutorial I could find and nothing will work - it always returns true. 我已经尝试过我可以找到的每个Stack Overflow / tutorial的大量变化,但是没有任何效果-它总是返回true。
//Test Example
var jsonData = JSON.parse(responseBody);
const schema = {
"required" : ["categories"],
"properties": {
"categories": {
"required" : ["aStringOne", "aStringTwo", "aStringThree" ],
"type": "array",
"properties" : {
"aStringOne": {"type": "string" },
"aStringTwo": {"type": "null" },
"aStringThree": {"type": "boolean" }
}
}
}
};
pm.test('Schema is present and accurate', () => {
var result=tv4.validateMultiple(jsonData, schema);
console.log(result);
pm.expect(result.valid).to.be.true;
});
//Response Example
{
"categories": [
{
"aStringOne": "31000",
"aStringTwo": "Yarp",
"aStringThree": "More Yarp Indeed"
}
]
}
This should return false, as all three properties are strings but its passing. 这应该返回false,因为所有三个属性都是字符串,但都是传递的。 I'm willing to use a different validator or another technique as long as I can export it as a postman collection to use with newman in my CI/CD process.
我愿意使用其他验证程序或其他技术,只要我可以将其导出为邮递员收藏以与CI / CD流程中的newman一起使用即可。 I look forward to any help you can give.
我期待您能提供任何帮助。
I would suggest moving away from using tv4 in Postman, the project isn't actively supported and Postman now includes a better (in my opinion), more actively maintained option called Ajv . 我建议不要在Postman中使用tv4 ,该项目不受积极支持,并且Postman现在包括一个更好的(更积极)维护的名为Ajv的选项。
The syntax is slightly different but hopefully, this gives you an idea of how it could work for you. 语法略有不同,但希望,这给你的可能是如何为你工作的想法。
I've mocked out your data and just added everything into the Tests
tab - If you change the jsonData
variable to pm.response.json()
it will run against the actual response body. 我已经模拟了您的数据,并将所有内容添加到“
Tests
选项卡中-如果将jsonData
变量更改为pm.response.json()
,它将在实际响应主体上运行。
var jsonData = {
"categories": [
{
"aStringOne": "31000",
"aStringTwo": "Yarp",
"aStringThree": "More Yarp Indeed"
}
]
}
var Ajv = require('ajv'),
ajv = new Ajv({logger: console, allErrors: true}),
schema = {
"type": "object",
"required": [ "categories"],
"properties": {
"categories": {
"type": "array",
"items": {
"type": "object",
"required": [ "aStringOne", "aStringTwo", "aStringThree" ],
"properties": {
"aStringOne": { "type": "string" },
"aStringTwo": { "type": "integer"},
"aStringThree": { "type": "boolean"},
}
}
}
}
}
pm.test('Schema is valid', function() {
pm.expect(ajv.validate(schema, jsonData), JSON.stringify(ajv.errors)).to.be.true
});
This is an example of it failing, I've included the allErrors
flag so that it will return all the errors rather than just the first one it sees. 这是一个失败的例子,我包含了
allErrors
标志,以便它将返回所有错误,而不仅仅是返回它看到的第一个错误。 In the pm.expect()
method, I've added JSON.stringify(ajv.errors)
so you can see the error in the Test Result
tab. 在
pm.expect()
方法中,我添加了JSON.stringify(ajv.errors)
以便您可以在“ Test Result
选项卡中看到错误。 It's a little bit messy and could be tidied up but all the error information is there. 有点混乱,可以整理一下,但是所有错误信息都在那儿。
Setting the properties to string
show the validation passing: 将属性设置为
string
显示通过验证:
If one of the required Keys
is not there, it will also error for this too: 如果所需的
Keys
不存在,它也将为此错误:
Working with schemas is quite difficult and it's not easy to both create them (nested arrays and objects are tricky) and ensure they are doing what you want to do. 使用模式非常困难,而且创建它们(嵌套数组和对象很棘手)并确保它们正在执行您想做的事情并不容易。
There are occasions where I thought something should fail and it passed the validation test. 在某些情况下,我认为某些事情应该失败,并且它通过了验证测试。 It just takes a bit of learning/practising and once you understand the schema structures, they can become extremely useful.
只需一点学习/实践,一旦您了解了架构结构,它们就会变得非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.