QA Interview Hub · Knowledge base · Quizzes · Українська

API › Scripts and checks in Postman

Schema validation and a chain of requests

Schema validation with pm.response.to.have.jsonSchema checks the fields and types of the whole response and catches contract drift. Business values are checked by separate tests. additionalProperties: false forbids extra fields, but it is kept only where a leak is a risk, because elsewhere new fields would cause false failures. In a chain, post-response saves the id to a variable after checking the status, and the next request uses it as {{accountId}}.

API · Scripts and checks in Postman

A test checks one field and the backend added an extra one. How do you notice the contract change?

A backend developer added an internalId field to the response and changed amount from a number to the string "150.00". A test that checks body.status stayed green while clients break. Single expect calls only see the fields you looked at. Schema validation looks at the whole structure. The lesson covers what it catches, what it does not, and how to pass a request's result to the next one.

How it works and why it is designed so

pm.response.to.have.jsonSchema(schema) checks the response body against a JSON Schema: required fields, types, allowed values, format. A schema describes the shape of data, not specific values, so one test protects the contract against drift while values are checked by separate tests. The schema is usually taken from the OpenAPI specification rather than written by hand, so it does not diverge from the documentation.

Example: a schema that catches an extra field and a type change

const schema = {
  type: 'object',
  required: ['id', 'status', 'amount', 'currency'],
  properties: {
    id: { type: 'string' },
    status: { enum: ['pending', 'completed', 'failed'] },
    amount: { type: 'number', exclusiveMinimum: 0 },
    currency: { type: 'string', pattern: '^[A-Z]{3}$' },
  },
  additionalProperties: false, // an extra field = a failed test
};
pm.test('Schema is valid', () => {
  pm.response.to.have.jsonSchema(schema);
});

A response with amount: "150.00" now fails on the type, and a response with internalId fails on additionalProperties: false. The report names the path to the field, so the cause is visible without debugging. It also protects against leaking internal fields.

A chain of requests

The result of the first request goes to the next one through a variable: stored in post-response, substituted as {{accountId}}.

A chain of two requests

POST /accounts201, the response has an idaccountIdthe variable lives between requestsGET /accounts/{{accountId}}reads what the first one created
Figure 1. Data between requests goes through a variable, not by hand.
// Post-response of POST /accounts
if (pm.response.code === 201) {
  pm.environment.set('accountId', pm.response.json().id);
}

// Post-response of the last request in the folder: clean up
pm.environment.unset('accountId');

Note the unset at the end: without it the next run starts with someone else's id.

Limits and alternatives

additionalProperties: false cuts both ways: a new optional field is usually not a breaking change, yet the test rejects it. Turn it on where the response composition is strict or there is a leak risk, and elsewhere allow extra fields. A schema does not check business values: amount: 1 instead of 150 passes. It also describes only the body, so status and headers are checked separately. For big contracts keep the schema in one place and validate with ajv in code, as in the lesson on schema validation.

Common mistakes

  • A schema written "to fit the response". It was generated from the current response, so it confirms any defect. Take it from the contract;
  • A leftover id. The variable stayed from the last run and the next request works with someone else's data;
  • A chain without checking each step. The first request failed and the next ones hurry on with an empty {{accountId}} and give confusing failures. Check the status before storing the id;
  • A schema instead of values. It does not see a wrong amount or status.

Check yourself

The server returned amount: "150" instead of 150, and the test checks only pm.expect(body.amount).to.eql(150). It will fail. How does a schema help more here?

Answer

A value test fails only on this field. A schema checks all fields at once: types, required, extra keys. So it catches drift in the fields nobody wrote a separate expect for.

When does additionalProperties: false hurt rather than help?

Answer

When the API evolves by adding fields: a new optional field does not break clients, yet the test goes red. There it is better to allow extra fields and keep the strict schema for sensitive responses.

In an interview. Junior: A schema checks the whole structure of a response: fields, types, values. additionalProperties: false catches extra fields. I pass data between requests through variables: store the id in post-response and use {{accountId}}. Middle/Senior: A schema catches contract drift but not business values, so it is complemented with value tests. I keep the strict ban on extra fields where there is a leak risk, since otherwise additive changes cause false failures. In a chain I check the status before storing the id and clean up the variables so runs do not depend on each other.

More articles in this chapter