Schema validation helps check if the data you get matches the expected format. It makes sure your app works with correct and complete data.
0
0
Schema validation basics in Postman
Introduction
When testing an API response to confirm it has all required fields.
When verifying that data types in a response are correct, like numbers or strings.
When checking if optional fields are present or absent as expected.
When ensuring nested objects or arrays in the response follow the right structure.
When automating tests to catch changes in API response format early.
Syntax
Postman
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.
Examples
This example checks that the response has a userId as a number and username as a string, both required.
Postman
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);
});This example validates that the response is an array of objects, each with an id and title.
Postman
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);
});Sample Program
This test checks that the API response for a product has all required fields with correct types.
Postman
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); });
OutputSuccess
Important Notes
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.
Summary
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.