简体   繁体   English

使用TV4的邮递员架构验证

[英]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. 有点混乱,可以整理一下,但是所有错误信息都在那儿。

邮递员失败1

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不存在,它也将为此错误:

邮递员失败2

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.

相关问题 无法使用 tv4 验证完整的 JSON 架构 - Unable to validate complete JSON schema using tv4 邮差Json Schema验证失败 - Postman Json Schema validation failed 在Postman中测试响应模式的泛型函数 - Generic function to test response schema in Postman Refference JSON schema from "define" API YAML file in Postman instead of creating a variable with actual JSON - Refference JSON schema from "define" API YAML file in Postman instead of creating a variable with actual JSON 如何在Postman中的不同集合中存储和重用架构? - How to store and reuse schema within different collections in Postman? 使用MotioCI进行数据验证 - Data validation using MotioCI 使用邮递员在多个循环中设置环境变量 - Set env variables in multiple loops using postman 如何使用 SSO oauth 2.0 登录,然后存储令牌以从邮递员自动化脚本运行所有其他 API? - How to login using SSO oauth 2.0 and then storing token to run all other API from postman automation script? 如何使用Postman验证响应正文中的节点是否可能包含null或字符串? - How to verify if a node that might either contain a null or a string in the response body using Postman? 如何使用邮递员读取和声明CSV响应 - How to read and assert response from CSV using postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM