Issue in Validating schema in postman with multiple same arrays

Hi all,

I’m trying to validate the response JSON schema in postman which is failing. There are 2 problem areas which I suppose-

In response, there is a dynamic value e.g., SKU319, which schema converter is considering as a property for the array.

The response has multiple similar objects in the array i.e.,-

{
    "skuCode": "SKU319",
    "location": "AMZ",
    "qty": 0.000,
    "committedQty": 0.000,
    "dedicatedInv": []
},
{
    "skuCode": "SKU319",
    "location": "BWD",
    "qty": 0.000,
    "committedQty": 0.000,
    "dedicatedInv": []
},
{
    "skuCode": "SKU319",
    "location": "CAL",
    "qty": 0.000,
    "committedQty": 0.000,
    "dedicatedInv": []
}

The number of times the above objects can appear is not fixed.
Here is the complete response JSON which I need to validate in schema-

{
    "responseCode": 0,
    "responseMessage": "Success",
    "response": {
        "totalSkus": "1",
        "totalPages": 1,
        "currentPage": 1,
        "inventoryMap": [
            {
                "SKU319": [
                    {
                        "skuCode": "SKU319",
                        "location": "AMZ",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "BWD",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "CAL",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "CHE",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "DWK",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "FAZ",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "GUW",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "KOL",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "S71",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "SDP",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "SNP",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    },
                    {
                        "skuCode": "SKU319",
                        "location": "TRET",
                        "qty": 0.000,
                        "committedQty": 0.000,
                        "dedicatedInv": []
                    }
                ]
            }
        ]
    }
}

Please help to how to validate this schema. Tried multiple times but still failing. Thanks!

Hey @swatimaan :wave:

What does the schema look like that’s not working for you?

Those are just the responses.

Hi @danny-dainton

Here is the schema-

pm.variables.set("schema", {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "responseCode": {
      "type": "integer"
    },
    "responseMessage": {
      "type": "string"
    },
    "response": {
      "type": "object",
      "properties": {
        "totalSkus": {
          "type": "string"
        },
        "totalPages": {
          "type": "integer"
        },
        "currentPage": {
          "type": "integer"
        },
        "inventoryMap": {
          "type": "array",
          "items": [
            {
              "type": "object",
              "properties": {
                "": {
                  "type": "array",
                  "items": [
                    {
                      "type": "object",
                      "properties": {
                        "skuCode": {
                          "type": "string"
                        },
                        "location": {
                          "type": "string"
                        },
                        "qty": {
                          "type": "number"
                        },
                        "committedQty": {
                          "type": "number"
                        },
                        "dedicatedInv": {
                          "type": "array",
                          "items": {}
                        }
                      },
                      "required": [
                        "skuCode",
                        "location",
                        "qty",
                        "committedQty",
                        "dedicatedInv"
                      ]                      
                    }
                  ]                    
				}
              },
              "required": []
            }
          ]
        }
      },
      "required": [
        "totalSkus",
        "totalPages",
        "currentPage",
        "inventoryMap"
      ]
    }
  },
  "required": [
    "responseCode",
    "responseMessage",
    "response"
  ],
  "additionalProperties": false
});

What does the test code look like that’s validating the response? What’s failing?

You’re better off sharing all the details of what you’re doing / what you have so far as it will assist others to help you with solutions.

You need to bare in mind that no one can see what you have in front of you so over-sharing is alway better - be sure to mask any sensitive details before posting. :trophy:

1 Like

Would something like this work for you:

let schema = {
    "type": "object",
    "required": [
        "responseCode",
        "responseMessage",
        "response"
    ],
    "properties": {
        "responseCode": {
            "type": "integer"
        },
        "responseMessage": {
            "type": "string"
        },
        "response": {
            "type": "object",
            "required": [
                "totalSkus",
                "totalPages",
                "currentPage",
                "inventoryMap"
            ],
            "properties": {
                "totalSkus": {
                    "type": "string"
                },
                "totalPages": {
                    "type": "integer"
                },
                "currentPage": {
                    "type": "integer"
                },
                "inventoryMap": {
                    "type": "array",
                    "items": {
                        "anyOf": [
                            {
                                "type": "object",
                                "required": [
                                    "SKU319"
                                ],
                                "properties": {
                                    "SKU319": {
                                        "type": "array",
                                        "items": {
                                            "anyOf": [
                                                {
                                                    "type": "object",
                                                    "required": [
                                                        "skuCode",
                                                        "location",
                                                        "qty",
                                                        "committedQty",
                                                        "dedicatedInv"
                                                    ],
                                                    "properties": {
                                                        "skuCode": {
                                                            "type": "string"
                                                        },
                                                        "location": {
                                                            "type": "string"
                                                        },
                                                        "qty": {
                                                            "type": "number"
                                                        },
                                                        "committedQty": {
                                                            "type": "number"
                                                        },
                                                        "dedicatedInv": {
                                                            "type": "array"
                                                        }
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}


pm.test("Schema check", () => {
    pm.response.to.have.jsonSchema(schema)
});
1 Like

Thank you, Danny. I tried using the above solution but it is not working. So I’m sharing the collection with you.

It has 2 APIs, first is used to generate the SKU which is coming in the response requestkey field, which I’ve set in the skuCode2 variable, which is further used in the 2nd API.
So, every time I’ll hit the first API my skuCode2 will change and the response JSON for the 2nd API will change that is why the schema validation is failing.
I need to validate the schema for the 2nd API.

Please let me know if any more information is required from my side as I’m new to using this. Sharing the collection with you over message.

You should be able to capture that dynamic value from the format response and then use the variable syntax in the second request.

That way the key wouldn’t be hardcode and failing like it is now.

1 Like

Okay. Thank you, Danny. Let me try this solution.