John
John

Reputation: 99

Trying to use external reference in Postman with tv4

I'm trying to use an external reference in Postman and validating that with tv4. This is my code:

  var schema = tv4.getSchema('https://schema.getpostman.com/json/collection/v1/');
  console.log(tv4.validate(responseBody, schema);

and after testing I get

'TypeError Cannot read property '$ref' of undefined'

.

Does that mean my schema is not valid somehow?

Upvotes: 0

Views: 1280

Answers (1)

Alwin Kesler
Alwin Kesler

Reputation: 1520

I know it's late, but this could help others

tv4.getSchema(name) is used to retrieve an already loaded schema. tv4.addSchema(name, schema) is used to append a new schema of name with schema value

So, what should you do?

Reading this article I understood that you can't make two requests in a test using Postman. Instead you should store its value in a environment or global variable and don't use tv4's functions as those (I guess) were meant to be used in environments where you can actually download a schema using http module.

Finally, your example should look as this

var schema = JSON.parse(postman.getEnvironmentVariable('myEnvVarName'));
let valid = tv4.validate(pm.response.json(), schema, false, true);

Upvotes: 1

Related Questions