Ensuring the Accuracy of JavaScript Tests with JSON Schema Verification

An API I have provides responses in the following format:

[
{"id": 12345,
"value": "some_string",
"practice_id": "12344"},

{"id": 12346,
"value": "some_other_string",
"practice_id": "12345"},
]

I'm currently testing whether the response complies with a specific JSON-Schema, and my schema test looks like this:

response.body.should.have.schema({
        type: 'array',
        required: ['id', 'value', 'practice_id'],
        properties: {
            id: {
                type: 'number',
            },
            value: {
                type: 'string',
            },
            practice_id: {
                type: 'string',
                minLength: 5,
            }            
        }
    });

The problem I'm facing is that the test passes even if I modify the data type of id to string or change the value of practice_id to number, which is incorrect.

What am I missing here? I am utilizing Postman-BDD for validating the responses.

Answer №1

It appears that your schema may need to be adjusted to align more closely with the following structure:

{
  "type": "array",
  "items":
  {
    "required":
    [
        "id",
        "value",
        "practice_id"
    ],
    "properties":
    {
        "id":
        {
            "type": "number"
        },
        "value":
        {
            "type": "string"
        },
        "practice_id":
        {
            "type": "string",
            "minLength": 5
        }
    }
  }
}

One key aspect that seems to be missing is the inclusion of the "items" keywords in order to accurately define the array content. Additionally, this schema has been found to trigger an error when validating sample data using JSONBuddy: https://i.sstatic.net/58Ug5.png

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

What are the steps to disable the automatic interpretation of escaped unicode characters in Python 3's json module?

I am facing an issue with Python 3.6.5. In a string, there is an escaped unicode character (&), but when parsing it using the json module, the unicode character is not escaped as expected. How can I disable this behavior? The encoding flag, as per the ...

What is the reason behind the absence of DriveInfo's properties when converting to a JSON string with Json.NET?

I tried to serialize DriveInfo into a Json string using the following code, but it only returns the "name" property: DriveInfo dr = new DriveInfo("C"); string json = Newtonsoft.Json.JsonConvert.SerializeObject(dr); The resulting string is: {"_name":" ...

JSON data functioning correctly in one section of the script, yet encountering issues in a separate section

I'm facing an issue with the script below that seems to be causing some trouble: success: function( widget_data ) { if( widget_data.d[0] ) { var j = 0; for ( j = 0; j <= widget_data.d.length - 1; j++ ) { $.get ...

Is there a way to make a jQuery dialog function similarly to a JavaScript alert box?

The topic of replacing the standard 'alert' with a customized jQuery dialog has been discussed before, as seen in this thread: Custom alert and confirm box in jquery In that conversation, Hemant Malpote pointed out that: "But it [the suggested ...

Encountered JSON Parsing Issue (lack of knowledge in JSON handling)

As I am moving a system from one server to another, I encountered an error when trying to access a particular page. Strangely, the code is identical to the live version of the system where it works perfectly fine. The JavaScript code causing the error is ...

The componentDidMount lifecycle method of a rendered component in a Route is not being triggered

IMPORTANT: SOLUTION PROVIDED BELOW, NO NEED TO READ THROUGH THE QUESTION I am utilizing the following Router with react-router 4 return ( <Router> <Page> <Route exact path="/" component={HomePage} /> <Route path="/c ...

Phantom-node failing to return the value from the page.evaluate function

My goal is to extract a specific element from a webpage and save it as an image locally. Here is the node.js code I am using with phantom-node: var phantom = require('phantom'); phantom.create().then(function(ph) { ph.createPage().then(funct ...

Discover how to extract JSON data from a webpage even when the response is "None"

I need to retrieve data maps from their API, which are in JSON format. This is the code I have written: r = requests.get(url) if r.ok: soup = BeautifulSoup(r.content, 'lxml') soup.status_code j = json.loads(str(soup)) However, an e ...

Learn how to efficiently execute a function multiple times using pure JavaScript

I am trying to create a tabbed content functionality with multiple elements. How can I utilize the same function for various elements declared in a variable? For example, I want to clone the parent div.tabs element with similar content but different ids an ...

When attempting to deserialize JSON objects in parallel, the process fails even when the

Is it possible to deserialize JSON values in parallel using rayon? I encountered a situation where a valid JSON from the serde-json example fails when trying to deserialize inside par_iter, despite being parsed correctly without parallelization. Below is t ...

Having trouble saving sessions in Node.js using express-session

I am new to using node.js and working with sessions. I have been attempting to store certain values in the session, but every time I refresh the page and check the console.log, the session data has not been saved. Here is my code: var session; var org; f ...

Ensuring validation with Javascript upon submitting a form

Hey there, I'm looking to validate field values before submitting a form. Here's the code I have: <table width="600" border="0" align="left"> <tr> <script type="text/javascript"> function previewPrint() { var RegNumber = docum ...

Styling the Material UI ImageList with a fade scroll effect

I am currently working on a horizontal scrolling ImageList using Material UI V5, and I'm looking to create a smoother transition at the edges of the list. I have managed to fade the edges, but my goal is to only fade them when the user has reached the ...

Determine the data attribute values of all checkboxes using jQuery and store them in a string

I am faced with a challenge involving a series of checkboxes that are structured like this: <input type="checkbox" class="pcb" value="1" data-id="99"> <input type="checkbox" class="pcb" value="2" data-id="98"> <input type="checkbox" class=" ...

Storing historical MongoDB records

I'm facing a challenge where I need to keep my collection updated with only the documents from the last 3 months while ensuring that all saved documents are archived. After conducting some research, we came up with a potential solution: We plan to se ...

Javascript favorite star toggling

An excellent illustration is the star icon located on the left side of this post. You have the ability to click on it to save this message as a favorite and click again to remove the flag. I have already set up a page /favorites/add/{post_id}/, but I am ...

Having trouble getting JavaScript to work with the link_to Delete in Rails 4.1.8?

I'm currently working through the introductory Rails guide found at One issue I've encountered involves using the link_to method to delete an article. Despite following the instructions in section 5.13 of the guide, the server console indicates ...

Enhancing MaterialUI Card in React with custom expandable feature: learn how to dynamically change Card styles on expansion

There are a total of 20 cards displayed on this page. When using MaterialUI Card, the onExpandChange property allows for defining actions like this: <Card expandable={true} onExpandChange={this.clickHandle}> With this action, it is easy to deter ...

What is the process for linking an HTML document to another HTML document within a div using jQuery?

Check out my HTML code: <!DOCTYPE html> <html> <head> <title>Hilarious Jokes!</title> <meta charset="utf-8"> <link href="final%20project.css" rel="stylesheet"> <script src=" ...

Issues arise when Flowplayer fails to display properly and is unable to access the designated element

I have a code that enables FlowPlayer to retrieve a Javascript array and play the video files upon user click. However, I am facing two issues: The player and playlist are not being displayed on the page. An error related to Flowplayer's inability ...