# How to validate schema that have specific value and data type

**URL:** <https://community.postman.com/t/how-to-validate-schema-that-have-specific-value-and-data-type/64820>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [July 31, 2024, 2:46pm UTC](https://community.postman.com/t/how-to-validate-schema-that-have-specific-value-and-data-type/64820 "2024-07-31T14:46:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![htt261094](https://avatars.discourse-cdn.com/v4/letter/h/bcef8e/32.png) [@htt261094](https://community.postman.com/u/htt261094)\
**Post date:** [July 31, 2024, 2:46pm UTC](https://community.postman.com/t/how-to-validate-schema-that-have-specific-value-and-data-type/64820/1 "2024-07-31T14:46:50Z")

</div>

I’m trying to validate a schema that strictly follow in correct order. Here is my code:  
const schema = {  
“type”: “object”,  
“properties”: {  
“ob1”: { “type”: “integer” },  
“ob2”: { “type”: “string” },  
},  
};

pm.test(“Validate schema”, () =\> {  
pm.response.to.have.jsonSchema(schema);  
});

and the response from API is:  
{  
“fact”: “Cats are North America’s most popular pets: there are 73 million cats compared to 63 million dogs. Over 30% of households in North America own a cat.”,  
“length”: 149  
}

Now what is need is validate that in the const schema, there have to be a key named “fact” and “length” and it have to be appear in the correct order in the API response. But what I get is, whatever I change in schema, as long as it have in the response, my test will passed.

What I mean is, the code I wrote should be failed but it not and the correct that I want is:

const schema = {  
“type”: “object”,  
“properties”: {  
“fact”: { “type”: “string” },  
“length”: { “type”: “number” },  
},  
};

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [July 31, 2024, 3:23pm UTC](https://community.postman.com/t/how-to-validate-schema-that-have-specific-value-and-data-type/64820/2 "2024-07-31T15:23:34Z")

</div>

Hi @htt261094. Welcome to the Postman Community :postman_logo:!

JSON itself does not guarantee the order of properties in an object as it is considered an unordered key-value pair, so JSON Schema by extension does not validate if a certain key comes before another. You will have to do this check manually, and there are a couple of ways you could achieve it in JavaScript.

One way is to use `Object.keys(obj)` to extract the keys as an array and iteratively validate the position of each key against a Northstar array of keys. See example below.

```auto
const json = {
  "fact": "Cats are North America’s most popular pets: there are 73 million cats compared to 63 million dogs. Over 30% of households in North America own a cat.",
  "length": 149
};

const expectedOrder = ["fact", "length"];
const actualOrder = Object.keys(json);

const isValidOrder = expectedOrder.every((key, index) => actualOrder[index] === key);

console.log(isValidOrder ? "The order is correct." : "The order is incorrect.");

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 30, 2024, 3:23pm UTC](https://community.postman.com/t/how-to-validate-schema-that-have-specific-value-and-data-type/64820/3 "2024-08-30T15:23:50Z")

</div>

Closed on August 30, 2024, 3:23pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
