Reasons why Schema Validation is required! 😲
JSON Schema Validation in Postman is required because of the below reasons:
🔷 We keep an eye on API responses to ensure the format we receive is the same as expected.
🔷 It is simpler to verify that an API is returning valid data when we build a model of the API response using JSON Schema.
🔷 We receive alerts whenever a JSON response undergoes a breaking change.
Object Response in Schema Validation🤠
In this section of "Schema Validation in Postman," we will discuss the syntax with an example using Object Response.
In general, the JSON response looks like a curly brace, and the syntax is as follows:
{ }
And the syntax of JSON schema in Object Response is as follows:
const schema = { "type": "object" };
Syntax of testing a JSON schema in Object Response is:
pm.test("Validate schema", () => {
pm.response.to.have.jsonSchema(schema);
});
Optional Property of Object in Schema Validation 🧾
In this section of "Schema Validation in Postman," we will discuss the syntax of the Optional Property of Object in Schema Validation.
The JSON response of the Optional Property looks like the below:
{
"code": "FX002"
}
Syntax of JSON schema having type and property name.
const schema = {
"type": "object",
"properties": { "code": { "type": "string" }
}
};
We have a lot of options for the "type" field. For example:
👉🏻 number
👉🏻 null
👉🏻 array
👉🏻 string
👉🏻 boolean
👉🏻 Object
Required Property of Object in Schema Validation📂
In this section of "Schema Validation in Postman," we will discuss the syntax of Required Property of Object in Schema Validation.
In the below example, note that JSON schema with a property named "code" of type String is the required property:
Let's take a look at the given JSON response:
{
"code": "FX002"
}
Syntax of JSON schema with property name and type:
const schema = { "type": "object", "properties": { "code": { "type": "string" } }, "required": ["code"] };
Nested Objects in Schema Validation
In this section of "Schema Validation in Postman," we will discuss the syntax of Nested Object in Schema Validation.
Objects that are placed inside one another are called Nested Objects. Within a nested object, you can build other nested objects.
The syntax of Nested Objects is as follows:
JSON response:
{ "code": "2", "error": { "message": "Not permitted." } }
JSON Schema:
const schema = {
"type": "object",
"properties": {
"code": { "type": "string" },
"error": {
"type": "object",
"properties": {
"message": { "type": "string" }
}, "required": ["message"]
}
},
"required": ["code", "error"]
};
Frequently Asked Questions
How do you validate data in Postman?
Your collection needs to be connected to an API if you want your requests validated. If you create a collection from a schema or add it as a relation to an existing API, the collection is linked to the API. Postman checks a request before it is dispatched.
What is a JSON Schema file?
The IETF standard, known as JSON Schema, provides a format for the JSON data needed and how to interact with it. Applying such standards to a JSON document may ensure that related JSON data is consistent and accurate.
Why do we need JSON Schema validation?
The key benefit of JSON Schema is that it produces machine- and human-friendly documentation. The data structure can be precisely described in form developers can efficiently utilize for automated validation. This makes the job easier for testers and developers, but the benefits go beyond productivity.
Check this out : what is schema in dbms
Conclusion
We have discussed the topic of Schema Validation in Postman. We have seen why Schema Validation is important, Object response, Optional property, Required property, and Nested Objects in Schema Validation.
We hope this blog has helped you enhance your knowledge of Schema Validation in Postman. If you want to learn more, check out our articles Commands of JSON, JSON Clients in Redis, Developer Notes on Redis, and many more on our platform Coding Ninjas Studio.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!
Happy Learning!