# Negative number validation in json schema

**URL:** <https://community.postman.com/t/negative-number-validation-in-json-schema/56321>\
**Category:** Help Hub\
**Tags:** tests, schemavalidation\
**Created:** [January 8, 2024, 5:36pm UTC](https://community.postman.com/t/negative-number-validation-in-json-schema/56321 "2024-01-08T17:36:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shalaka-m](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@shalaka-m](https://community.postman.com/u/shalaka-m)\
**Post date:** [January 8, 2024, 5:36pm UTC](https://community.postman.com/t/negative-number-validation-in-json-schema/56321/1 "2024-01-08T17:36:01Z")

</div>

below is my json schema :

```auto
{
    "BRK.A": [
        {
            "time": "20231204T0930",
            "opn": -102.49,
            "hi": 1362.995704,
            "lo": 54.1,
            "cl": 1362.995704,
            "vl": 2421
        },
        {
            "time": "20231204T0945",
            "opn": 988.11,
            "hi": 834.120704,
            "lo": 608.51,
            "cl": 651.650704,
            "vl": 2908
        }
]
}

```

opn has a negative value which is invalid.  
So I would like to add a test to check for any negative values in response during schema validation.

I tried to add “minimum” in schema validation like below but it does not work.

```auto
const schema = {
  "type": "object",
  "properties": {
    "": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "time": {
            "type": "string"
          },
          "opn": {
            "type": "number",
            "minimum": 0
          }
},
        "required": [
          "time",
          "opn",
          "hi",
          "lo",
          "cl",
          "vl"
        ]
      }
    }
  }
}

```

Please help add schema validation for this.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 9, 2024, 1:54pm UTC](https://community.postman.com/t/negative-number-validation-in-json-schema/56321/2 "2024-01-09T13:54:37Z")

</div>

```auto
let response =
{
    "BRK.A": [
        {
            "time": "20231204T0930",
            "opn": -102.49,
            "hi": 1362.995704,
            "lo": 54.1,
            "cl": 1362.995704,
            "vl": 2421
        },
        {
            "time": "20231204T0945",
            "opn": 988.11,
            "hi": 834.120704,
            "lo": 608.51,
            "cl": 651.650704,
            "vl": 2908
        }
    ]
}

const schema = {
    "type": "object",
    "properties": {
        "BRK.A": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "time": {
                        "type": "string"
                    },
                    "opn": {
                        "type": "number",
                        "minimum": 0
                    }
                },
                "required": [
                    "time",
                    "opn",
                    "hi",
                    "lo",
                    "cl",
                    "vl"
                ]
            },
        }
    },
};

pm.test("Json Schema Validation", function () {
    pm.expect(response).to.have.jsonSchema(schema);
});

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/9/09af6234aea781c5767c245804633a55ba6d31f2.png)

Remove the - from the first opn value in the response, and it passes.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/9/29af2c6dcd165789cb98f5c43a597d7eaccd1e4d.png)

Remove the vl key from the from the second object in the response (to ensure that the required fields are being triggered).

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/6/c61a7bf457634192306debc409d62a6bfb123407.png)

---

<div class="post-metadata">

**Author:** ![shalaka-m](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@shalaka-m](https://community.postman.com/u/shalaka-m)\
**Post date:** [January 9, 2024, 2:31pm UTC](https://community.postman.com/t/negative-number-validation-in-json-schema/56321/3 "2024-01-09T14:31:26Z")

</div>

Thanks Mike for replying.

I want my schema validation test to fail when a negative value is returned in response.

how can I handle it in schema validation ?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 9, 2024, 3:06pm UTC](https://community.postman.com/t/negative-number-validation-in-json-schema/56321/4 "2024-01-09T15:06:26Z")

</div>

That is what the code I posted is doing.

Have a look at the failure message.

It’s failing because the opn value for the first object in the array should be \>=0.

Therefore the minimum:0 method is working as expected.

Please note, if you have a lot of objects in the response, then the error returned will get cut short and you probably won’t get details of all of the failures if there is more than one.

I don’t know how important that is.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [February 8, 2024, 3:06pm UTC](https://community.postman.com/t/negative-number-validation-in-json-schema/56321/5 "2024-02-08T15:06:26Z")

</div>

Closed on February 8, 2024, 3:06pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
