简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM