Challenges Ensuring Validation of Each Object in JSON Schema Using Postman

Despite its seemingly straightforward nature, I am encountering difficulties in fully validating JSON response data within Postman (Tiny Validator 4) when multiple objects are returned. During negative testing, I discovered that it only checks the first object instead of all the objects to ensure that all data is being properly returned. I want to verify that ALL data returned in ALL objects is of the correct type and present as expected. While this should theoretically not be a problem, I want to confirm for regression testing purposes in my test environment.

Response Example:

[
    {
        "productID": 1,
        "product": "Desktop",
        "versionID": 123,
        "version": "Win10 x64"
    },
    {
        "productID": 2,
        "product": "Laptop",
        "versionID": 321,
        "version": "Win 10 x64"
    },
    {
        "productID": 3,
        "product": "Monitor",
        "versionID": 456,
        "version": "LCD Panel"
    }
];

Postman Schema and Object Setup:

var schema = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "productID": {
          "type": "integer"
        },
        "product": {
          "type": "string"
        },
        "versionID": {
          "type": "integer"
        },
        "version": {
          "type": "string"
        }
      },
      "required": [
        "productID",
        "product",
        "versionID",
        "version"
      ]
    }
  ]
}

var jsonData = pm.response.json();

var wrongDataType = [
    {
        "productID": 1,
        "product": "Desktop",
        "versionID": 123,
        "version": "Win10 x64"
    },
    {
        "productID": "2",       //data returned as a string instead of integer
        "product": "Laptop",
        "versionID": 321,
        "version": "Win 10 x64"
    }
];

var dataMissing = [
    {
        "productID": 1,
        "product": "Desktop",
        "versionID": 123,
        "version": "Win10 x64"
    },
    {
        "productID": "2",
        //"product": "Laptop",      //data not present in response
        "versionID": 321,
        "version": "Win 10 x64"
    }
];

Test Validation using Tiny Validator 4:

pm.test('Schema is valid', function() {
  pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});

pm.test('Wrong data type is not valid', function() {
  pm.expect(tv4.validate(wrongDataType, schema)).to.be.false;
});

pm.test('Data missing is not valid', function() {
  pm.expect(tv4.validate(dataMissing, schema)).to.be.false;
});

In the TV4 validation of the returned jsonData against the schema, it returns true because the API response is valid. For subsequent comparisons involving wrongDataType and dataMissing against the schema, the Postman tests display failures due to TV4 validation expecting an invalid comparison, despite them actually matching.

If I introduce the wrong data type or missing data in the first object, the validation fails, and the test passes. However, if incorrect data is placed in any other returned object (as shown in the example), the test fails because it does not detect errors in the second objects.

How can I modify my tests to check all objects in a response and fail if any are incorrect or missing?

Answer №1

After some time, I realized that you have correctly defined items as an array. This indicates that each item is specified in order. With only one subschema present, only the first item will be checked.

To resolve your problem, try using the subschema without wrapping it in an array.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing JSON strings into Unicode representation

The title may be simple, but the description leads to a more complex issue. I am currently dealing with various Wordpress plugins and need to execute certain tasks upon form submission with the data received. The challenge lies in the format of the entry d ...

What is the process for transferring data between components in React?

Currently, I have set up a system to display an employee table from the database. Each record in the table includes an "edit" link which, when clicked, should trigger the display of a form below the table containing the corresponding records for editing. T ...

Is it beneficial to use both Bootstrap and ng-bootstrap together?

I have two modules in my angular website - "bootstrap" and "ng-bootstrap". Do I need both or just one? I am thinking of keeping only "ng-bootstrap" 4.0.0.0 and removing "bootstrap". Is this acceptable? What are the steps to remove "Bootstrap"? Can I simp ...

What could be causing the response from curl_exec() in my web service?

What is causing curl_exec() to return an array in my web service? I set up a web service for sending push notifications with Firebase. Everything functions perfectly except that the server response includes the notification result, even though I did not r ...

How can I use JavaScript (specifically Reactjs/NextJs) to add a new contact to my phone from a web

I'm currently working on creating a new contact using JS. I've come across documentation for retrieving all contacts from the phone here. Are there any libraries available to assist with this task, or is it only achievable with React Native? ...

Creating fresh texts by pairing the values in constants with variables

Make sure to carefully read the question before proceeding. I have attempted multiple commands in order to retrieve a single variable from the server [TSE](https://old.tsetmc.com/Loader.aspx?ParTree=15131F) which serves Stock Exchange information. The pag ...

Display the returned view following an AJAX request

Currently, I am trying to search for a specific date in the database using ajax to trigger the function. However, I am encountering a 404 error when I should be receiving the desired outcome, and the ajax call is entering an error mode. Here is the snippet ...

Guide to shutting down Bootstrap 5 modal using React

Incorporating bootstrap 5 with react in my project without installation, I am utilizing CDN links to integrate bootstrap elements into the DOM. The challenge arises when attempting to close the bootstrap modal after data is updated. Despite trying to uti ...

There was a SyntaxError in the PHP code while parsing JSON: it expected a comma or a closing curly brace after a property value in the object at line 1

test.php <?php require_once __DIR__ . '/vendor/autoload.php'; $urlPaczkomaty = "http://api.paczkomaty.pl/?do=listmachines_xml"; $curl = new Curl\Curl(); $curl->get( $urlPaczkomaty ); $xml = simplexml_load_string($cur ...

Show live updates in the input field

I'm struggling to get an input's results to update in real time within another disabled input field. Here is the code I'm working with: var inputBox = document.getElementById('input'); inputBox.onkeyup = function(){ document. ...

How can one use an ajax call to delete rows from a table and then add new rows in their

Hey everyone, I'm looking to make an AJAX call and when it's successful, I want to remove all existing elements in my table and replace them with new ones based on the returned data. This is the method I have for making the AJAX call: $(document ...

Puppeteer has a limit of running only three instances on Heroku

Currently, I am in the process of developing a website that employs puppeteer to extract information from another site. The npm server works flawlessly on my local machine, but once deployed on Heroku, it halts after processing the first three files. The ...

Encountering a ReactJs and TypeScript error: "menuItems.map is not a function, issue with map method"

Greetings! I am currently working on implementing the logic of using useState and mapping an array to show only one dropdown item at a time. Below is my array structure with tags (menu name, links (to router), icon (menu icon), and if there are dropdown i ...

Discover the magic of observing prop changes in Vue Composition API / Vue 3!

Exploring the Vue Composition API RFC Reference site, it's easy to find various uses of the watch module, but there is a lack of examples on how to watch component props. This crucial information is not highlighted on the main page of Vue Composition ...

Tips for verifying the existence of 'key' in jq prior to looping through the values

Whenever I run the query below, I encounter an error message saying Cannot iterate over null (null). This happens because the .property_history key is missing from the result object. Is there a way to first check if the .property_history key exists before ...

Creating an array of objects using a constructor: a step-by-step guide

a) Here is an example of an array containing objects with various properties: [ { "Account": "1111-000", "Desc": "Accrued - Payroll & Payroll Tax", "Amount": "-8,459.88", "Partner": "RAP", "FY": "2018", ...

What is the best way to retrieve a service response prior to component initialization in Angular 4?

My service sends a request to an API, and based on the response, I need to decide whether a component should be loaded or not. However, due to the delay in receiving the response, the component loads regardless of the response status. After about 0.5 secon ...

Verify if the array contains any empty elements

Is there a way to determine if an array contains empty elements? Consider the following array: var arr = [ 'a', 'b', , 'd']; In this case, arr[2] is undefined. It's important to check for such occurrences. One approach ...

Incomplete Backbone and Ajax request handling

When attempting to retrieve the value for rhOptions through an ajax call, I am encountering a problem where the view is rendering this value as "undefined". Below is a snippet of my View code: UserView.prototype.initialize = function() { var self; ...

What is the best way to view or save the content of a PDF file using a web service?

As a newcomer to web services and JavaScript, I am facing a challenge with calling a web service that returns a PDF file in a specific format. Here is the link to view the PDF: https://i.stack.imgur.com/RlZM8.png To fetch the PDF, I am using the following ...