Schema Validation
// Definition
Asserting that an API request or response matches a defined schema (JSON Schema, OpenAPI, Protobuf). Catches contract drift the moment it appears, without writing field-by-field assertions in every test.
// Code Example
import Ajv from 'ajv';
const schema = {
type: 'object',
required: ['id', 'name'],
properties: {
id: { type: 'string' },
name: { type: 'string' },
},
};
cy.request('/api/tools/cypress').then((res) => {
const valid = new Ajv().validate(schema, res.body);
expect(valid, 'response matches schema').to.be.true;
});// Related terms
Contract Testing
Verifying that two services agree on the shape of the messages they exchange. Catches breaking API changes without expensive end-to-end tests across multiple deployed services.
REST
Representational State Transfer — an architectural style for HTTP APIs where resources are addressed by URLs and manipulated via standard HTTP verbs (GET, POST, PUT, DELETE). The dominant API style for over a decade.
GraphQL
A query language and runtime for APIs where clients specify exactly which fields they want in a single request. Replaces multiple REST endpoints with one flexible endpoint and a typed schema.
Learn more · API Testing Masterclass
Chapter 5 · Lesson 2: JSON Schema Validation