JSON Validation

How exactly can JSON validation be done?
I wanted to check the values passed in the JSON schema, is it possible?

2, Could I re-use functions under my test ?

Hi. I think you might find this blog post useful. It has 10 tips for API testing. Tip #4 shows how to validate responses against a JSON Schema, and Tip #5 shows how to re-use JavaScript code in Postman.

In addition, we will be adding support for collection-level scripts very soon, which will make re-using code even easier.

{
  "name": "Paul",
  "gender": "Male",
  "age": 28,
  "country": "England"
}

Ok, given I hit a GET request and it returns the above response.
First parse the response so we can interact with it:

var response = JSON.parse(responseBody);
tests["Age is 26"] = response.age == 28;
tests["Age value type is number"] = typeof response.age == "number";

The above test tests that the age value is 28 and it also checks that it’s type is number. So if the value was somehow “28” inside quotations, it would fail as it’s now a string.

However, using 3 equals sign in a test will also test it’s type so you’re probably fine using the one test:

var response = JSON.parse(responseBody);
tests["Age is 26"] = response.age === 28;

Is that what you were asking? :S

2 Likes