0
0
Postmantesting~5 mins

JSON Schema validation in Postman

Choose your learning style9 modes available
Introduction

JSON Schema validation helps check if the data you get matches the expected format. It makes sure your app works with correct and complete information.

When testing an API response to confirm it has the right fields and data types.
When you want to catch errors early by validating input data before processing.
When you need to ensure that changes in the API do not break your app.
When automating tests to quickly verify many responses without manual checks.
Syntax
Postman
pm.test('Validate JSON schema', () => {
    const schema = {
        type: 'object',
        properties: {
            id: { type: 'integer' },
            name: { type: 'string' },
            active: { type: 'boolean' }
        },
        required: ['id', 'name', 'active']
    };
    pm.response.to.have.jsonSchema(schema);
});

The schema defines the expected structure and data types.

pm.response.to.have.jsonSchema() runs the validation on the response body.

Examples
This checks that the response has a username and a valid email string.
Postman
pm.test('Check user schema', () => {
    const userSchema = {
        type: 'object',
        properties: {
            username: { type: 'string' },
            email: { type: 'string', format: 'email' }
        },
        required: ['username', 'email']
    };
    pm.response.to.have.jsonSchema(userSchema);
});
This ensures the product has an ID, price, and optionally a list of string tags.
Postman
pm.test('Validate product schema', () => {
    const productSchema = {
        type: 'object',
        properties: {
            productId: { type: 'integer' },
            price: { type: 'number' },
            tags: { type: 'array', items: { type: 'string' } }
        },
        required: ['productId', 'price']
    };
    pm.response.to.have.jsonSchema(productSchema);
});
Sample Program

This test checks if the API response matches the user schema with id, name, and isActive fields.

Postman
pm.test('Validate simple user JSON schema', () => {
    const userSchema = {
        type: 'object',
        properties: {
            id: { type: 'integer' },
            name: { type: 'string' },
            isActive: { type: 'boolean' }
        },
        required: ['id', 'name', 'isActive']
    };
    pm.response.to.have.jsonSchema(userSchema);
});
OutputSuccess
Important Notes

Make sure your JSON schema matches the exact structure of the response.

Use required to specify which fields must be present.

Postman uses Ajv library internally for JSON schema validation.

Summary

JSON Schema validation checks if data matches expected format.

It helps catch errors early and automate response checks.

Postman makes it easy to add schema tests in your API tests.