Check datatype of a response body

{
    "size": 3,
    "response": [
        {
            "tenant_id": "ABC1234",
            "alert_id": "123456789"
        }
     ]
}

This is my response body, I need to check if my alert_id field datatype is array type or integer type checking with OR conditon would be better way of writing for me…need urgent help.

I have tried lot of methods ajv tv4 and typeOf method and many more can not tell how many…

pm.test("Check type", () => {
    _.each(pm.response.json().response, (item) => { 
        pm.expect(typeof item["alert_id"]).to.be.oneOf(['number', 'array']);
    })
}) 

Would something like this help you out?

thanks @dannydainton for your quick response appreciated that…your code works for me but when i change the value form number to string then also the testcase get pass even in that condition also what to do…also for some test cases where the response is null there the .to.be.oneOf([‘number’,‘null’]); value testcase get passed. Also i have tried your code before telling it before from you i mean i have tried lots of methods before…

You mentioned trying Ajv, what was the code you used that didn’t work?

How about using something like this:

let schema = {
    "type": "object",
    "required": [
        "size",
        "response"
    ],
    "properties": {
        "size": {
            "type": "integer"
        },
        "response": {
            "type": "array",
            "items": {
                "anyOf": [
                    {
                        "type": "object",
                        "required": [
                            "tenant_id",
                            "alert_id"
                        ],
                        "properties": {
                            "tenant_id": {
                                "type": "string"
                            },
                            "alert_id": {
                                "type": ["number", "array"]
                            }
                        }
                    }
                ]
            }
        }
    }
}

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

Yes sir i have tried something like this also but still doesn’t worked for me.

If that doesn’t work then you’re missing details in your question or not running something correctly.

Can you ensure that the example response is the actual response body.

Can you provide a screenshot of it not working in Postman so we can see what’s happening, please?

It’s difficult to suggest where to go from here with only ‘it doesn’t work’ :joy:

hi @dannydainton below i have mentioned the tried ajv methods but these all failed.
var Ajv = require(‘ajv’),

// ajv = new Ajv({logger: console}),

// schema = {

// “properties”: {

// “user_name”: {

// “type”:[“array”, “null”]

// }

// }

// };

// var Ajv = require(‘ajv’),

// ajv = new Ajv({logger: console}),

// schema = {

// “properties”: {

// “parentName”: {

// “type”:[“string”, “null”]

// }

// }

// };

// pm.test(‘Verify parentName is string’, function() {

// var resParentName = pm.response.json().demo[0].parentName;

// pm.expect(ajv.validate(schema, {parentName: resParentName})).to.be.true;

// });

var schema = {

“items”: {

"type": ["array","null"]

}

};

// pm.test(‘Schema is valid’, function() {

// console.log(pm.expect(tv4.validate(data1, schema)).to.be.true);

// });

// pm.test(“Validate schema by shyam new”, () => {

// for(var i = 0; i < respSize; i++){

// var data1 = [jsonData.response[i].user_name];

// //var data2 = [jsonData.response[i].alert_score];

// }

// pm.response.to.have.jsonSchema(data1,schema);

// });

// var Ajv = require(‘ajv’),

// ajv = new Ajv({ logger: console, allErrors: true }),

// schema = {

// “type”:“object”,

// “required”:[

// “user_name”,

// “alert_score”,

// “message”,

// “confidence_score”,

// “assigned_to”,

// “recommendation”

// ],

// “properties”:{

// “user_name”:{

// “$id”:"#/properties/User_name",

// “type”:[“array”,“null”]

// }

// }

// }

// pm.test(‘success shyam’, function() {

// pm.expect(ajv.validate(schema, pm.response.json()), JSON.stringify(ajv.errors)).to.be.true;

// });

// pm.test(‘again datatype check’, function() {

// for(var i = 0; i < respSize; i++){

// pm.expect( typeof(jsonData.response[i].user_name)).to.be.oneOf([ “array”,“null”]);

// }

// });

Are you able to share a screenshot of the Postman app showing the scheme and test that I previously posted and where the error is please?

It’s difficult to know what’s wrong with just seeing a script you’ve used and nothing else :grin:

okey i am sharing the screen shot pls have a look







pls see last three images for more info

@dannydainton sir i want to have skype call with u that would be better for my and your understanding too and that would be very helpful for me to ask questions can u share ur skype number or ID.

right now i have not shared ajv tv4 methods cause there were too long and need lot of time to fix it.

Try this:
pm.test(“Check type”, () => {
_.each(pm.response.json().response, (item) => {
pm.expect(typeof item[“alert_id”]).to.be.oneOf([‘string’,‘object’]);
})
})

thanks @ashutosh.verma1 for replying.
i have tried this before but the problem is that if alet_id has null as a response then there is only way to check is using object data type there is no way to check nulll as a datatype. Checking null as a datatype always fails i have tried it. So can u provide a better way.