postman

Is it possible in a Postman function to pass an 'enum' How would I define it.?


I have this Postman scenario. What I'd like to do is create an enum or the equivalent for the two or three schemas I look for in response bodies (i.e. success, fault, etc.) So far, I have one success and two failure schemas. My Java background is trying to refactor this into a 'enum' if that's possible. I'm trying to avoid just "knowing" what schemas can be expected

utils.validateResponseSchema(pm, "successSchema")

My function works as such.

validateResponseSchema: (pm, expectedSchema) => {
    pm.test("Response schema validation successful", () => {
        pm.expect(pm.response.json()).to.have.jsonSchema(pm.collectionVariables.get(expectedSchema))
    })
}

I'm trying to refactor the 'expectedSchema' parameter into an 'enum' of sorts. That's my question in a nutshell, and where would I define it in Postman?

To define the various expected response schemas in an Enum...


Solution

  • You can achive this by define as a Global Variable

    If you prefer, you can define the enum directly in a global variable:

    1. Go to "Manage Environments" in Postman.

    2. Create a new global variable called ResponseSchemas.

    3. Set its value to the stringified enum object:

      { "SUCCESS": "successSchema", "FAULT_A": "faultSchemaA", "FAULT_B": "faultSchemaB" }

    retrieve and use the ResponseSchemas enum:

    validateResponseSchema: (pm, expectedSchemaKey) => {
        const responseSchemas = JSON.parse(pm.environment.get("ResponseSchemas"));
        const expectedSchema = responseSchemas[expectedSchemaKey];
        
        pm.test("Response schema validation successful", () => {
    
    
    pm.expect(pm.response.json()).to.have.jsonSchema(pm.collectionVariables.get(expectedSchema));
    });
    

    }