1

I've run across several use cases where I want to store a function in a Postman collection variable and reference it from an endpoint. Here's one example:

// at collection level
const parameterizedSchema = (codes) => ({
  type: 'object',
  properties: {
    code:  { enum: codes    },
    label: { type: 'string' }
  }
});
pm.collectionVariables.set('parameterizedSchema', parameterizedSchema)
// in endpoint
const genericSchema = pm.collectionVariables.get('parameterizedSchema');
const thisEndpointSchema = genericSchema(['foo', 'bar']);

pm.test('response has correct schema', function () {
  const {data} = pm.response.json();
  if (!tv4.validate(data, thisEndpointSchema)) {
    console.error(tv4.error);
    pm.expect.fail()
  }
});

But I don't seem to be able to store functions in variables -- I get an error TypeError: paramterizedSchema is not a function.

Is it possible to store functions in Postman variables somehow?

Sasgorilla
  • 2,403
  • 2
  • 29
  • 56
  • Does this answer your question? [How to Write Global Functions in Postman](https://stackoverflow.com/questions/45673961/how-to-write-global-functions-in-postman) – Christian Baumann Mar 25 '22 at 14:28

0 Answers0