# Issue in Validating schema in postman with multiple same arrays

**URL:** <https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116>\
**Category:** Help Hub\
**Tags:** schemavalidation\
**Created:** [March 8, 2021, 4:36am UTC](https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116 "2021-03-08T04:36:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![swatimaan](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@swatimaan](https://community.postman.com/u/swatimaan)\
**Post date:** [March 8, 2021, 4:36am UTC](https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116/1 "2021-03-08T04:36:54Z")

</div>

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.,-

```auto
{
    "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-

```auto
{
    "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!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 8, 2021, 10:21am UTC](https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116/2 "2021-03-08T10:21:57Z")

</div>

Hey @swatimaan 👋

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

Those are just the responses.

---

<div class="post-metadata">

**Author:** ![swatimaan](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@swatimaan](https://community.postman.com/u/swatimaan)\
**Post date:** [March 8, 2021, 1:53pm UTC](https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116/3 "2021-03-08T13:53:47Z")

</div>

Hi @danny-dainton

Here is the schema-

```auto
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
});

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 8, 2021, 2:03pm UTC](https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116/4 "2021-03-08T14:03:20Z")

</div>

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. 🏆

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 8, 2021, 2:46pm UTC](https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116/5 "2021-03-08T14:46:06Z")

</div>

Would something like this work for you:

```auto
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)
});

```

---

<div class="post-metadata">

**Author:** ![swatimaan](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@swatimaan](https://community.postman.com/u/swatimaan)\
**Post date:** [March 8, 2021, 4:46pm UTC](https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116/6 "2021-03-08T16:46:07Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/7/2768ddcf64bdf086eddb77426d3d774bf2212050.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/c/8c4507484e1e88670a041336eb8078b665cbf487.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 8, 2021, 6:20pm UTC](https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116/7 "2021-03-08T18:20:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![swatimaan](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@swatimaan](https://community.postman.com/u/swatimaan)\
**Post date:** [March 9, 2021, 7:31am UTC](https://community.postman.com/t/issue-in-validating-schema-in-postman-with-multiple-same-arrays/21116/8 "2021-03-09T07:31:10Z")

</div>

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