Schema validation helps check if the data you get matches the expected format. It makes sure your app works with correct and complete data.
Schema validation basics in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
pm.test('Validate response schema', () => { const schema = { type: 'object', properties: { id: { type: 'number' }, name: { type: 'string' }, active: { type: 'boolean' } }, required: ['id', 'name', 'active'] }; pm.response.to.have.jsonSchema(schema); });
Use pm.response.to.have.jsonSchema(schema) to check the response matches the schema.
The schema follows JSON Schema standards to describe expected data types and required fields.
const schema = {
type: 'object',
properties: {
userId: { type: 'number' },
username: { type: 'string' }
},
required: ['userId', 'username']
};
pm.test('User schema validation', () => {
pm.response.to.have.jsonSchema(schema);
});const schema = {
type: 'array',
items: {
type: 'object',
properties: {
id: { type: 'number' },
title: { type: 'string' }
},
required: ['id', 'title']
}
};
pm.test('Posts array schema validation', () => {
pm.response.to.have.jsonSchema(schema);
});This test checks that the API response for a product has all required fields with correct types.
pm.test('Validate product response schema', () => { const productSchema = { type: 'object', properties: { productId: { type: 'number' }, productName: { type: 'string' }, price: { type: 'number' }, inStock: { type: 'boolean' } }, required: ['productId', 'productName', 'price', 'inStock'] }; pm.response.to.have.jsonSchema(productSchema); });
Always define required fields to catch missing data early.
JSON Schema supports many data types and formats; use them to make your tests precise.
If the schema validation fails, Postman test will show which part of the response did not match.
Schema validation checks if response data matches expected structure and types.
Use Postman's pm.response.to.have.jsonSchema() for easy validation.
Defining clear schemas helps catch errors and keeps API responses reliable.
Practice
Solution
Step 1: Understand schema validation concept
Schema validation ensures the response data structure and types match what is expected.Step 2: Compare options with schema validation purpose
Only To check if the response data matches the expected structure and data types describes checking data structure and types, which is the core of schema validation.Final Answer:
To check if the response data matches the expected structure and data types -> Option DQuick Check:
Schema validation = check structure and types [OK]
- Confusing schema validation with performance testing
- Thinking schema validation checks URL correctness
- Assuming schema validation checks server status
schema variable?Solution
Step 1: Recall correct Postman syntax for schema validation
The correct method ispm.response.to.have.jsonSchema(schema);to validate JSON schema.Step 2: Check each option for syntax correctness
Only pm.response.to.have.jsonSchema(schema); matches the exact Postman syntax; others are invalid method names.Final Answer:
pm.response.to.have.jsonSchema(schema); -> Option AQuick Check:
Correct method = jsonSchema() [OK]
- Using incorrect method names like jsonSchemaCheck
- Mixing up method chaining order
- Omitting 'to.have' in the assertion
{
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"}
},
"required": ["id", "name"]
}What will happen if the API response is
{"id": 10, "name": "Alice"}?Solution
Step 1: Analyze the schema requirements
The schema expects an object with 'id' as integer and 'name' as string, both required.Step 2: Compare the response with schema
The response has 'id' as 10 (integer) and 'name' as "Alice" (string), satisfying all requirements.Final Answer:
The schema validation will pass successfully -> Option BQuick Check:
Response matches schema types and required fields [OK]
- Confusing integer with string type
- Assuming missing fields when all are present
- Ignoring required fields in schema
const schema = {
type: "object",
properties: {
age: { type: "integer" }
},
required: ["age"]
};
pm.test("Schema is valid");
pm.response.to.have.jsonSchema(schema);But the test always fails even when the response has an integer
age. What is the likely error?Solution
Step 1: Check the test function usage
The correct syntax ispm.test("name", () => { assertion });but the code callspm.test("Schema is valid");without the required callback function.Step 2: Check assertion placement
The assertionpm.response.to.have.jsonSchema(schema);is outside thepm.testcallback and will not be properly associated with the test.Final Answer:
The assertion should be insidepm.testcallback, but the code usespm.testincorrectly -> Option AQuick Check:
pm.test requires callback containing assertion [OK]
- Calling assertions directly without pm.test wrapper
- Misspelling pm.test or using wrong test function
- Not wrapping assertion inside pm.test callback
id (integer) and optional email (string). Which JSON schema correctly validates this response array?Solution
Step 1: Identify the response type
The response is an array of user objects, so schema type must be "array" with "items" describing each object.Step 2: Check object properties and requirements
Each object must have "id" as integer (required) and "email" as optional string (not required).Step 3: Validate options against requirements
{ "type": "array", "items": { "type": "object", "properties": { "id": {"type": "integer"}, "email": {"type": "string"} }, "required": ["id"] } } correctly defines an array with items as objects, "id" integer required, "email" string optional. Others either define object instead of array, wrong types, or require "email".Final Answer:
{ "type": "array", "items": { "type": "object", "properties": { "id": {"type": "integer"}, "email": {"type": "string"} }, "required": ["id"] } } -> Option CQuick Check:
Array of objects with required id integer and optional email string [OK]
- Defining response as object instead of array
- Marking optional fields as required
- Using wrong data types for properties
