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

Sample Json Schema

{

"body": ERROR: NOT FOUND: Valid,

"meta": {

    "status": "success",

    "message": "Valid"
}

}

I’m using the following codes and getting the error, can any one help me on this issue

var jsonData = JSON.response;

var Ajv = require(‘ajv’),

ajv = new Ajv({

    logger: console,

    allErrors: true

}),

schema = {

    "type": "Object",

    "required": [

        "body",

        "meta"

    ],

    "properties": {

        "body": {

            "$body": "#/properties/body",

            "type": "string"

        },

        "meta": {

            "$body": "#/properties/meta",

            "type": "array",

            "status": {

                "$body": "#/properties/meta/status",

                "type": "string",

            },

            "message": {

                "$body": "#/properties/meta/message",

                "type": "string"

            }

        }

    }

}

pm.test(‘Schema is valid’, function() {

pm.expect(ajv.validate(schema, jsonData), JSON.stringify(ajv.errors)).to.be.true;

});

Error Message–
Error: schema is invalid: data.type should be equal to one of the allowed values, data.type should be array, data.type should match some schema in anyOf

Danny,

Perhaps not strictly related question: what are the option if developers needs to reuse schema between different collections? Today we just copy and paste the same schema definition in each collection, is there a way to share it maybe as a environment variable?

1 Like