Ajv json schema validator returns true when the json schema is actually invalid

ajv json schema validator returns true when the json schema is actually invalid
Notice that dayAdjustment propery must be of type boolean, not string

var Ajv = require('ajv'),
ajv = new Ajv(),
schema = {
    "items": {
    "type": "object",
    "required": [
      "id",
      "userUpdate",
      "dateUpdate",
      "timeEntryId",
      "approved",
      "trcId",
      "projectId",
      "workOrderId",
      "laborId",
      "timeIn",
      "timeOut",
      "adjustmentHours",
      "hours",
      "totalCost",
      "dayAdjustment",
      "lastUpdatedBy",
      "lastUpdatedDate"
    ],
    "properties": {
      "workOrderId": {
        "type": "integer"
      },
      "id": {
        "type": "integer"
      },
      "userUpdate": {
        "type": "string"
      },
      "approved": {
        "type": "boolean"
      },
      "dateUpdate": {
        "type": "string"
      },
      "timeEntryId": {
        "type": "integer"
      },
      "trcId": {
        "type": "integer"
      },
      "projectId": {
        "type": "number"
      },
      "laborId": {
        "type": "number"
      },
      "timeIn": {
        "type": "string"
      },
      "timeOut": {
        "type": "string",
      },
      "adjustmentHours": {
        "type": "number"
      },
      "hours": {
        "type": "number",
      },
      "dayAdjustment": {
        "type": "string",
      },
      "lastUpdatedBy": {
        "type": "string"
      },
      "lastUpdatedDate": {
        "type": "string"
      },
      "totalCost": {
        "type": "number"
      }
    }
  }
};

var validate = ajv.compile(schema);
var valid = validate(response);

pm.test('Check that the response is correct', function() {
pm.expect(valid).to.be.true;
});

JSON

 {
            "id": 81,
            "userUpdate": "API_TEST",
            "dateUpdate": "20201128043927",
            "timeEntryId": 81,
            "dateWork": "20201125000000",
            "trcId": 1,
            "projectId": 458124,
            "laborId": 100023699,
            "adjustmentHours": 0.0,
            "hours": 0.0,
            "dayAdjustment": false,
            "approved": false,
            "lastUpdatedBy": "API_TEST",
            "lastUpdatedDate": "20201128043927"
        }

image

This video explains this behavior.

Your schema doesn’t look right to me. The top level property shouldn’t be items. Try taking everything out of the items property

Yes, the problem was with schema. items is redundant in this case
Thank you for the responses!

1 Like