Checking a schema with tv4

Hello,

I’m trying to test a response body schema with tv4.
My test scrip I’m using is:

var schema = {
  "type": "object",
  "properties": {
            "Id": { "type": "string"},
            "Progress": { "type": "string"},
            "Status": { "type": "string"},
            "Info": { "type" : "boolean"},
            "Created": {"type": "string","format": "date-time"},
            "Ended": {"type": "boolean"},
            "Operation": {"type": "string"},
            "Requested": {"type": "array",
                "items" :{
                    "type": "string"
                }
            },
  },
};

var user = JSON.parse(responseBody);
tests ["Success"] = tv4.validate(user, schema);

And the response body I get is:

{
    "Id": "29b61d38-1353-4c7f-9e8a-9f115ddef37f",
    "Progress": "Queued",
    "Status": "InProgress",
    "Info": null,
    "Created": "2019-09-03T00:00:00",
    "Ended": null,
    "Operation": "CalculateHolidayRightsCommand",
    "Requested": [
        "59101105848"
    ]
}

I’m getting a false error.

Hey @Coxjeffrey,

I would recommend using Ajv over tv4, this also comes as part of the Postman app.

You can use this example to check your response against the schema:

var Ajv = require('ajv'),
    ajv = new Ajv({ logger: console, allErrors: true }),
    schema = {
   "type":"object",
   "required":[
      "Id",
      "Progress",
      "Status",
      "Info",
      "Created",
      "Ended",
      "Operation",
      "Requested"
   ],
   "properties":{
      "Id":{
         "$id":"#/properties/Id",
         "type":"string"
      },
      "Progress":{
         "$id":"#/properties/Progress",
         "type":"string"
      },
      "Status":{
         "$id":"#/properties/Status",
         "type":"string"
      },
      "Info":{
         "$id":"#/properties/Info",
         "type":"null"
      },
      "Created":{
         "$id":"#/properties/Created",
         "type":"string"
      },
      "Ended":{
         "$id":"#/properties/Ended",
         "type":"null"
      },
      "Operation":{
         "$id":"#/properties/Operation",
         "type":"string"
      },
      "Requested":{
         "$id":"#/properties/Requested",
         "type":"array",
         "items":{
            "$id":"#/properties/Requested/items",
            "type":"string"
         }
      }
   }
}

pm.test('Schema is valid', function() {
    pm.expect(ajv.validate(schema, pm.response.json()), JSON.stringify(ajv.errors)).to.be.true;
});