Negative number validation in json schema

below is my json schema :

{
    "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.

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.

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);
});

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

image

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

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 ?

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.

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