How to check all Keys in JSON are camelcase , for below request , containing Array of objects

Hi All,

Is it possible to check that all keys inside postman API response are camel case?

For example, Below API request response:

{
    "success": true,
    "data": [
        {
            "requestUuid": "RequestUuid5425",
            "messages": [
                {
                    "messageType": "INFO",
                    "messageCode": "AE-INF1000",
                    "messageDisplayText": "Identity saved successfully.",
                    "priority": 0
                }
            ],
            "active": true,
            "objectValid": true,
            "success": true,
            "validated": false,
            "isNew": false
        }
    ],
    "numberOfElements": 0,
    "totalPages": 0,
    "totalElements": 0,
    "pageNumber": 0,
    "pageSize": 0
}

I need to check all Keys i.e.
1.success

Inside array objects:

2.requestUuid
3.messageType
4.messageCode
5.messageDisplayText

and outer keys :

numberOfElements
totalPages

Check all are camel case.

i have tried below test in postman Tests

let validate = name => !(!name.match(/^[a-z][A-Za-z]*$/));

pm.test("Verify camle case", function call() {
    var obj = pm.response.json();
    function walk(obj) {
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                var val = obj[key[0]];
                console.log(key + ":" + validate(key));
                walk(val);
            }
        }
    }
    walk(obj);
}
);

but it is only checking outer JSON keys. Screenshot below.

Any help is appreciated…
Thanks in Advance

Hey @jiten.khanna ,

Welcome to the community :clap:

For this you have to pull the data object after response.json. Your code will work properly if it looks like the one below. :+1:

let validate = name => !(!name.match(/^[a-z][A-Za-z]*$/));

pm.test("Verify camel case", function call() {
    var obj = pm.response.json().data;
    function walk(obj) {
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                var val = obj[key[0]];
                console.log(key + ":" + validate(key));
                walk(val);
            }
        }
    }
    walk(obj);
}
);   

Hi @yusuftaymann thanks for the help , but as per test shared it started checking only response inside data array ,
My requirement is that it should automatically test all the outer keys as well as inner keys.

After using your code it started displaying like this.

Now it is not checking for

“success”: true,

“messages”: [

            {

                "messageType": "INFO",

                "messageCode": "AE-INF1000",

                "messageDisplayText": "Identity saved successfully.",

                "priority": 0

            }

        ],

“numberOfElements”: 0,

"totalPages": 0,

"totalElements": 0,

"pageNumber": 0,

"pageSize": 0

}

Kindly help with same , so that it will check for all nested as well as unested JSON keys.

Thanks in advance

I’m not sure of the full context about why you need to check for camelcase but could you just validate the response against a schema to cover that too?

let schema = {
    "type": "object",
    "required": [
        "success",
        "data",
        "numberOfElements",
        "totalPages",
        "totalElements",
        "pageNumber",
        "pageSize"
    ],
    "properties": {
        "success": {
            "type": "boolean"
        },
        "data": {
            "type": "array",
            "items": {
                "anyOf": [
                    {
                        "type": "object",
                        "required": [
                            "requestUuid",
                            "messages",
                            "active",
                            "objectValid",
                            "success",
                            "validated",
                            "isNew"
                        ],
                        "properties": {
                            "requestUuid": {
                                "type": "string"
                            },
                            "messages": {
                                "type": "array",
                                "items": {
                                    "anyOf": [
                                        {
                                            "type": "object",
                                            "required": [
                                                "messageType",
                                                "messageCode",
                                                "messageDisplayText",
                                                "priority"
                                            ],
                                            "properties": {
                                                "messageType": {
                                                    "type": "string"
                                                },
                                                "messageCode": {
                                                    "type": "string"
                                                },
                                                "messageDisplayText": {

                                                    "type": "string"
                                                },
                                                "priority": {
                                                    "type": "integer"
                                                }
                                            }
                                        }
                                    ]
                                }
                            },
                            "active": {
                                "type": "boolean"
                            },
                            "objectValid": {
                                "type": "boolean"
                            },
                            "success": {
                                "type": "boolean"
                            },
                            "validated": {
                                "type": "boolean"
                            },
                            "isNew": {
                                "type": "boolean"
                            }
                        }
                    }
                ]
            }
        },
        "numberOfElements": {
            "type": "integer"
        },
        "totalPages": {
            "type": "integer"
        },
        "totalElements": {
            "type": "integer"
        },
        "pageNumber": {
            "type": "integer"
        },
        "pageSize": {
            "type": "integer"
        }
    }
}

pm.test("Schema is valid", () => {
    pm.response.to.have.jsonSchema(schema)
})

Hi @danny-dainton, thanks for the reply, actually we have a project requirement which includes some 80 different API’s and we need to test that all of them has camelCase.

it is not possible for us to create a schema for all of them, so we are looking for some postman function which can automatically iterate through all of them.

we are able to achieve it using simple JS and recursion, but not able achieve same in Postman.

JS code:

function iterateArray(array) {
	for(var i = 0; i < array.length; i++) {
    var dataobj = array[i];
		for (var key in dataobj) {
			console.log(key);
			if (dataobj.hasOwnProperty(key) && Array.isArray(dataobj[key])) {
				iterateArray(dataobj[key])
			}
			
		}
	}	
}


for (var key in obj) {
console.log(key);
console.log(Array.isArray(obj[key]));

  if (obj.hasOwnProperty(key) && Array.isArray(obj[key])) {
    iterateArray(obj[key])
  }
}

if you can suggest how will same thing work in Postman.

Also, is postman supports Recursion?

Thanks in Advance

Is checking the response of the API the best place to check if it returns camelcase properties?

Would a check at a lower level and closer to the code that creates the structure, be a more efficient solution?

Hi All,

we are able to achieve the solution.

pasting the code for someone’s future refrence.

let validate = name => !(!name.match(/^[a-z][A-Za-z]*$/));

pm.test("Iterate Camel Case", function () {
    var obj = pm.response.json();
    for (var key in obj) {
        if (validate(key) == false) {
            pm.expect.fail("Test Failed");
        }
        else {
            console.log(key + " : " + validate(key));
        }
        if (obj.hasOwnProperty(key) && Array.isArray(obj[key])) {
            iterateArray(obj[key])
        }
    }
    function iterateArray(array) {
        for (var i = 0; i < array.length; i++) {
            var dataobj = array[i];
            for (var key in dataobj) {
                if (validate(key) == false) {
                    pm.expect.fail("Test Failed");
                }
                else {
                    console.log(key + " : " + validate(key));
                }
                if (dataobj.hasOwnProperty(key) && Array.isArray(dataobj[key])) {
                    iterateArray(dataobj[key])
                }
            }
        }
    }
});
1 Like