简体   繁体   English

如何在错误消息中使用邮递员返回键/值进行AJV验证

[英]How to make ajv validation using postman return key/value in error message

Background 背景

I'm using ajv with postman for json schema validation. 我正在将带有邮递员的ajv用于json模式验证。 It works well, however it only gives me a single fail/pass result for an entire schema validation, rather than the exact failing key/value pair: 它运作良好,但是对于整个架构验证,它只给我一个失败/通过结果,而不是确切的失败键/值对:

var Ajv = require('ajv'),
    ajv = new Ajv({logger: console}),
    schema = {
        "properties": {
                "data" : {
                     "type": "object",
                     "properties" : {
                            "categories" : {
                                   "type": "array",
                                   "items" : [
                                           {"type": "object",
                                            "properties" : {
                                                    "id": {"type": "number"},
                                                    "ref": {"type": ['null', 'string']},
                                                    "parent_id": {"type": ['null', 'number']},
                                                    "image": {"type": ['null', 'string']},
         ...

  pm.test('Schema is valid', function() {
    var data = pm.response.json()['data'];
    pm.expect(ajv.validate(schema, {data: data})).to.be.true;

});

在此处输入图片说明

Question

How can I make avj/postman return the exact wrong key/pair validation? 我怎样才能使avj / postman返回正确的错误密钥/对验证?

update 更新

For this to work, the avj object must be declared like so: 为此,必须像这样声明avj对象:

var Ajv = require('ajv'),
ajv = new Ajv({logger: console,
            allErrors: true,
            verbose: true
}),

There is the ajv.errors object, that holds the information. 有一个保存信息的ajv.errors对象。

https://github.com/epoberezkin/ajv#validation-errors https://github.com/epoberezkin/ajv#validation-errors

For example, by adding something like this to the pm.expect() statement, it should show the message part of the error in the Postman Tests Results: 例如,通过在pm.expect()语句中添加类似的pm.expect() ,它应该在Postman Tests Results中显示错误的消息部分:

pm.expect(ajv.validate(ajv.validate(schema, {data: data}), JSON.stringify(ajv.errors[0].message)).to.be.true;

That's only going to show the first object in the array so you'd need to capture this in a different way if you wanted to iterate through that data. 那只会显示数组中的第一个对象,因此如果要遍历该数据,则需要以其他方式捕获它。

The correct way to print in the console the error message is by adding a an extra parameter to validate(). 在控制台中打印错误消息的正确方法是通过向validate()添加一个额外的参数。 First declare a "printErrors" function at the beginning of your Postman script tests: 首先在Postman脚本测试的开始声明一个“ printErrors”函数:

function printErrors(ajv){
    if(ajv.errors !== null){
        console.log(JSON.stringify(ajv.errors[0]));    
    }
}

Then add the a call to validate, like this: 然后添加一个调用以进行验证,如下所示:

var ajv = new Ajv({logger: console,
        allErrors: true,
        verbose: true
}),
yourData = {"your Data"}
yourSchema = {"your schema"};
pm.test('Schema is valid', function() {
    pm.expect(ajv.validate(schema, yourData), printErrors(ajv)).to.be.true;
});

You will see the log in the console box. 您将在控制台框中看到日志。 To open it you have to click on the console button, the third one starting from the left. 要打开它,您必须单击控制台按钮,第三个按钮从左侧开始。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM