How to handle reserved keywords

tests

#1

Hello! I’m trying to test a JSON schema which includes a reserved keyword.

"type": {
    "type": "string"
}

My test is failing and I narrowed it down to this. I’m assuming it’s because “type” is a reserved keyword. Is there a way this can be handled? Thank you in advance!


#2

Hey @Linda.Kim1990 – I wasn’t able to reproduce type being a keyword in the Javascript execution environment we expose in the Tests/Pre-request Script section of a request.

Something like the snippet below should log the value of the key-value pair in question.

let response = pm.response.json();
console.log("typeVar", response.type.type);

Are there any errors populating in the Postman console?

Alternatively, bracket notation should work.

let response = pm.response.json();
console.log("testingLog", response['type']['type']);

#3

Hi @Chris ! Thank you for your response. I added your script and it returned with:
TypeError | Cannot read property ‘type’ of undefined


#4

Would you be able to provide the script you’re using along with a snippet of the entire JSON response?

Feel free to redact any sensitive values. The error would indicate that we’re trying to access a key-value pair that doesn’t exist.


#5

@Chris perhaps I’m not using your script correctly because I tried it with the other fields and got the same thing.

Test Script:

const expectedSchema = {
    "avatar": {
        "type": "string"
    },
    "createdAt": {
        "type": "string"
    },
    "id": {
        "type": "string"
    },
    "meta": {
        "address": {
            "type": "string"
        },
        "address_hash": {
            "type": "string"
        },
        "agentId": {
            "type": "string"
        },
        "agent_email": {
            "type": "string"
        },
        "company": {
            "type": "string"
        },
        "first_name": {
            "type": "string"
        },
        "follow_email": {
            "type": "string"
        },
        "last_name": {
            "type": "string"
        },
        "listingCacheKey": {
            "type": "string"
        },
        "listingId": {
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "phone": {
            "type": "string"
        },
        "realtor_group_id": {
            "type": "string"
        },
        "relationship_id": {
            "type": "string"
        },
        "source": {
            "type": "string"
        }
    },
    "name": {
        "type": "string"
    },
    "notified": {
        "type": "boolean"
    },
    "notifiedText": {
        "type": "boolean"
    },
    "type": {
        "type": "string"
    },
    "updatedAt": {
        "type": "string"
    },
    "userId": {
        "type": "string"
    }
};

    var actualSchema = JSON.parse(responseBody);
    tests["Activities schema is valid"] = tv4.validate(actualSchema.data[0], expectedSchema)

    let response = pm.response.json();
    console.log("typeVar", response.data.type.type);

Response Body:

"data": [
        {
            "avatar": "initials",
            "createdAt": "2019-03-13T16:03:26.625+00:00",
            "id": "abc",
            "meta": {
                "address": "123 Fake Street, Orange CA 91745",
                "address_hash": "abc",
                "agentId": null,
                "agent_email": "abc@gmail.com",
                "company": "ABC Inc",
                "first_name": "Linda",
                "follow_email": "abc@gmail.com",
                "last_name": "Kim",
                "listingCacheKey": "abc@gmail.com",
                "listingId": "0000",
                "name": "Linda Kim",
                "phone": "555-270-3964",
                "realtor_group_id": "abc",
                "relationship_id": "abc",
                "source": "abc"
            },
            "name": "abc",
            "notified": false,
            "notifiedText": true,
            "type": "abc",
            "updatedAt": "2019-03-13T17:01:01.075+00:00",
            "userId": "abc"
        }