简体   繁体   中英

How can I log all failures in tv4.validateMultiple to Test Results tab with correct fail status

I have been able to get the error messages produced from tv4.validateMultiple to show in the Test Results, but I cannot figure out how to set them to show fail status.

var jsonData = JSON.parse(responseBody);
var schema = {...contains multiple error}
var results = tv4.validateMultiple(jsonData, schema);

if(results.valid){
    pm.test('Response has valid schema') = true;

} else {
    for (var i = 0; i < results.errors.length; i++) {
        pm.test("Contract: JSON Response has invalid schema in path [" + results.errors[i].dataPath + " | " + results.errors[i]); 
    }
}

Multiple messages are logged in the Test Results tab, but have a status of pass. With the 'old style' Postman tests[], you can set it = false. But that can't be used in a for loop because the test will end on first fail. pm.test will continue but the status is pass.

I tried placing the pm.test('Schema is valid', function() {pm.expect...} in a for loop but get the message "Don't make functions in a loop" message. So I tried pulling the function part outside, which didn't work either.

I've also tried Ajv and setting allErrors: true.

All I want to do is validate an entire response and report all failures in the Test Tab and xtrareports.

I'm sure this can be improved, but the following works.

var jsonData = JSON.parse(responseBody);

var schema = {
    "type": "object",
    "properties": {
        "data": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "field1": { "type": "string", "pattern": uuid_format },
                "field2": { "type": "string", "pattern": date_format },
                "field3": { "type": "string" },
                "field4": { "type": "string", "enum": ["ACTIVE", "INACTIVE"]},
            }
        },
        "pages": { "type": ["string", "null"]
        }
    }
};

var result = tv4.validateMultiple(jsonData, schema);

if (result.valid){
    pm.test("Contract: JSON Response has valid schema"), setTestStatus;

} else {
    for (var i = 0; i < result.errors.length; i++) {
        pm.test('JSON Response is INVALID ' + result.errors[i].dataPath +" " + result.errors[i].message, setTestStatus);

    }

}

function setTestStatus() {
    pm.expect(result.valid).to.be.true;
}

I was unable to get the correct pass/fail status with ajv, which I would like to use instead of tv4. I'm sure it has something to do with my regex or some detail like that even though I tested the regex pattern/response value and the schema with online validators. But here it is anyway.

var uuid_format = '/^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/i' 

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

var jsonData = JSON.parse(responseBody);
var schema = {...see above...}

var valid = ajv.validate(schema, jsonData);

if(valid){
    pm.test('Contract: JSON Response is valid', setTestStatus);

} else {
    for (var i = 0; i < ajv.errors.length; i++) {
        pm.test('JSON Response is INVALID ' + ajv.errors[i].dataPath +" " + ajv.errors[i].message, setTestStatus);
    }
}

function setTestStatus() {
    pm.expect(result.valid).to.be.true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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