0

I want to add a payload schema to an environment variable so that I can validate the response payload against the schema.

I have my environment variable defined as follows:

responseSchema: 
{ "properties":
      "firstname":
         {"type":"string" },
       "lastname": 
          {"type":"string"},
       "phonenumber": 
          {"type":"integer","format":"int64"}
       },
   "required":["firstname", "lastname", "phonenumber"]}

However, I can not access this environment variable within my Postman Test code. I've tried accessing it by:

environment.responseSchema

However, this returns null. How can I access the environment variable that I've created using postman. The way I have implemented this is consistent with http://blog.getpostman.com/2017/07/28/api-testing-tips-from-a-postman-professional/ TIP #4: JSON Schema validation

fuzzi
  • 1,967
  • 9
  • 46
  • 90

1 Answers1

3

So to be clear you are adding a collection variable, not an environment variable. More on Postman variables

To access your collection variable you can do pm.variables.get("responseSchema") in the Tests script tab.

To be a bit more complete you should parse it as well.

var mySchema = JSON.parse(pm.variables.get("responseSchema"));
console.log(mySchema.properties.firstname.type);

Also I believe your object is invalid you probably meant to do

{
    "properties": {
        "firstname": {
            "type": "string"
        },
        "lastname": {
            "type": "string"
        },
        "phonenumber": {
            "type": "integer",
            "format": "int64"
        }
    },
    "required": ["firstname", "lastname", "phonenumber"]
}
Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38