How to test a value returns null or a string

Hi,

I’m the following test to check my json response is structured correctly.

How can I check that the property ‘field_apply_url’ returns as either null or a string? I’m struggling to find a way how to do it.

const jsonData = pm.response.json();

pm.test("Apprenticeship details response is structured correctly", () => {

  pm.expect(jsonData).to.be.an("object");

  pm.expect(jsonData).to.have.property('nid').to.be.a('string');

  pm.expect(jsonData).to.have.property('field_apply_url').to.be.a('string');

/or/

 pm.expect(jsonData).to.have.property('field_apply_url').to.be.null;

    })

Thanks
Chris

Hi @chriswall02, one way to do is:

pm.expect(typeOf jsonData.field_apply_url==='string' || jsonData.field_apply_url=== null).to.be.true;

Can you try this.

1 Like
   pm.expect(Object.prototype.toString.call(a)).to.be.oneOf(["[object String]","[object Null]"])

The problem with above recommend approach is that type of null, array and {} arre the same “Object

so you can use this new approach. we are finding the class of the the object using prototype.toString().call()

Object.prototype.toString() - JavaScript | MDN

But the recommended way is to use schema validation than explictly validating type in your tests

1 Like