Postman is encountering issues with validating the JSON schema, despite the data being considered valid

Struggling to validate a response against a JSON schema? Even if the schema is valid, it always seems to fail. Let's take a look at the JSON data and the validation code in Postman:

{
    "items": [
        {
            "uuid": "f68ad4ba-a11e-485d-a2d7-17b9b07bd8d3",
            "name": "Code",
            "type": "app_code",
            "description": "Code 1",
            "content_type": "application/javascript",
            "audit": {
                "created_date": "2017-11-02T00:16:58.000Z",
                "created_by": "e7c97dc1-08eb-45ef-b883-d100553bac5c"
            }
        },
        ...
    ],
    "metadata": {
        "total": 124,
        "count": 3
    },
    "links": [
        ...
    ]
}

To validate this data, this code snippet is used in Postman:

const objectSchema = {
  "items": [
    ...
  ],
  "links": [
    ...
  ],
  "metadata": {}
};


pm.test("JSON schema validation", function() {
  var responseData = JSON.parse(responseBody);
  var result = tv4.validate(responseData, objectSchema, false, true);
  if (result !== true) {
      console.log('Schema validation failed:', tv4.error);
  }
  pm.expect(result).to.be.true;
  console.log(JSON.stringify(result));
});

If you encounter an error message like "Unknown property (not in schema)" in the console, how can you get more detailed information about what's causing the error? Any tips or solutions would be greatly appreciated!

Answer №1

Expanding on @Pavlo's feedback:

In your specific test scenario:

pm.test("Validation of JSON schema", function() {
  var responseData = JSON.parse(responseBody);
  var outcome = tv4.validate(responseData, objectSchema, false, true);
  if (outcome !== true) {
      console.log('Schema validation unsuccessful:', tv4.error);
  }
  pm.expect(outcome).to.be.true;
  console.log(JSON.stringify(outcome));
});

The line

var responseData = JSON.parse(responseBody);
requires substitution with:

var responseData = pm.response.json();

Additionally, following this helpful resource, you can include the following code snippet to pinpoint where the schema encountered an issue:

console.log(tv4.error.dataPath);

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

Problem with Decimal Math calculations in AngularJS and JavaScript

I am experiencing an issue with JavaScript calculations. On inspecting the fiddle, I noticed that the answer is displaying as rounded with no decimals, which is confusing to me. How can I modify my code to display the decimals of $scope.total? var mome ...

What is the process for activating JavaScript and embedding it into an HTML document?

I am currently utilizing a JavaScript API that contains several functions. How can I incorporate it into an HTML file? One of the functions, "api.ping()", performs well on PowerShell, but I am encountering difficulties with displaying it on an HTML file. ...

Does the onchange function in the dropdown list only work when the first item is changed?

Here is a snippet of my HTML code featuring a list item generated from a database using a foreach loop: <select class="form-control select" id="inventoryitem" name="inventoryitem" onchange="getunit();"> <option>---Select an item---</o ...

Exploring Apollo Client's invalidateQueries feature

React Query provides a functionality called invalidateQueries which allows us to designate cached data as outdated and triggers a refetch of related queries. Is there a similar feature available in Apollo Client? I currently have a list of entities that I ...

Utilize JavaScript and CSS to create dynamic text animations

(() => { const paraElement = document.querySelector('#animated-text'); const spanElement = paraElement.querySelector('span'); const wordsList = JSON.parse(spanElement.dataset.words); let counter = 0; setInterval(funct ...

Leverage the power of jQuery's .filter() method to pinpoint and target specific text

HTML: <p class="greeting"> hello, my name is kevin. what's yours? </p> jQuery: $("p.greeting").filter(function (){ return $this.text() === "my name is"; }).css("background", "green"); I am attempting to find and highlight the phra ...

The application experiences crashes when the tablet is rotated, happening while in the development stage

For my web-based Android application project, I am utilizing PhoneGap and Eclipse IDE on a Windows platform. My focus is specifically on Tablet development, more precisely on the Samsung Galaxy Tab 10.1. To develop, I use Eclipse and test the app on the ...

Encountering the issue, "Unable to convert BSON type objectId to String" while attempting MongoDB aggregation operations

I need to extract the most recent message from each conversation involving User 1 and User N. While attempting to achieve this, I encountered an error with my code. How can I resolve this issue using ObjectIds instead of strings which are stored in the da ...

Error occurred while trying to authenticate the user "root" with the password in Linux using NodeJS, Express, and PostgreSQL

Update - Hurrah! It appears I neglected to consult the manual. Following the guidelines exactly for the environmental variables seems to be necessary. Corrected code: # PostgreSQL Database Information PGDATABASE_TEST = user_db PGDATABASE = user_db PGUSER ...

How to dynamically reduce the number of columns in a textarea using jQuery according to its content

Does anyone know how to make a textarea shrinkwrap the text inside on blur? The default number of columns is 10 or 15 and I want the textarea to adjust its width based on the text content. I have tried the following code: $('textarea').on(&apos ...

Ways to divide different paths within a React Application

In my index.js file, I currently have the following code: <Router routes={routes} /> I want to move the routes section to a separate file. Here's what I've tried so far: routes.js export default ( <div> <Route path= ...

Ways to dynamically display commas and periods based on certain conditions?

I have three items that will be displayed conditionally. I need to include commas between them, with a period at the end if it's the last item. {this.state.a&&i++} {this.state.b&&i++} {this.state.c&&i++} {this.state.a && (i==1 ...

``Using `fs.lstat` function to check the status of a symbolic link that points to

When using the fs.stat function in my Windows environment (x64) on a symlink pointing to a directory, I encounter an error. Contrastingly, when running the same code on Appveyor (ia32), the fs.stat function for a symlink pointing to a directory works with ...

The drop-down component is being filled with data but unfortunately, only dummy data is functional at the

Having trouble populating data in the drop-down component. Everything works fine when using dummy JSON data as shown in the comments. The GET request service retrieves the necessary data, and then I assign the response to the appropriate variable. The GET ...

Retrieve populated information from Mongoose and send it to the client

While working on the server, I successfully populated user-data and printed it to the console without any issues. However, I am facing a challenge in accessing the data on the client side or even through the Playground of GraphQL. Below is my Schema setup ...

Fetching Data from JSON Object Using PHP

My goal is to extract specific objects from the data stored in a .json file. I am particularly interested in accessing the individual items within the stylers object. However, I am encountering difficulties in retrieving information related to color or vis ...

Creating a Login Form with Node.js and MongoDB

Currently, I am working on a node.js application that is connected to a remote mongoDB server. Inside the database are specific custom codes that have been created and shared with selected users. The main objective is to restrict access to the rest of the ...

Erase the white backdrop from the dragImage

Is there a way to remove the white outline that appears when dragging a draggable element from a dark background? I want to make it completely transparent. Here is a visual representation: https://i.sstatic.net/Eywf9.png Here is the code snippet: docume ...

What are some ways to integrate JavaScript code within an AngularJS environment?

For AngularJS, I am trying to run this code snippet. The model function does not seem to execute when I attempt to do so. No alerts or console messages are displayed at all. Is there a specific technique for incorporating script files into AngularJS? B ...

Create specification for the properties of the child component

I am interested in working with the props of a parent element's children and validating those props. Can I achieve this using prop-types? export default function MyComponent(props) { return ( <div> {React.Children.map(props.chil ...