Validating API Responses
Overview
Validate API responses against OpenAPI schemas, JSON Schema definitions, and contract specifications to detect data integrity violations, schema drift, and backward compatibility regressions. Run validation in middleware (development/staging) or as post-deployment contract tests to ensure every response conforms to the documented API contract.
Prerequisites
- OpenAPI 3.0+ specification with complete response schema definitions for all endpoints
- JSON Schema validator: Ajv (Node.js), jsonschema (Python), or everit-org/json-schema (Java)
- Response validation middleware or test harness integrated into CI pipeline
- API test client for exercising endpoints and capturing response bodies
- Schema diff tool for detecting contract changes between versions
Instructions
- Read the OpenAPI specification using Read and extract all response schemas per endpoint, including success responses (200, 201), error responses (400, 404, 500), and header definitions.
- Compile JSON Schema validators for each endpoint-status combination, enabling strict mode (
additionalProperties: false) to detect undocumented fields leaking into responses.
- Implement response validation middleware that intercepts outgoing responses and validates the body against the corresponding schema, logging violations without blocking responses in production.
- Configure validation strictness per environment:
strict (fail on violation) in development/staging, warn (log only) in production, with violation metrics emitted for monitoring.
- Add header validation to verify required response headers (Content-Type, Cache-Control, rate limit headers) match the OpenAPI specification.
- Build a contract test suite that sends representative requests to each endpoint and validates every response field, including nested objects, arrays, enums, nullable fields, and format constraints (date-time, email, URI).
- Implement schema drift detection that compares the current response shape against the documented schema after each deployment, alerting when undocumented fields appear or documented fields disappear.
- Generate a validation coverage report showing which endpoints and response codes have schema validation, identifying gaps in the specification.
See ${CLAUDESKILLDIR}/references/implementation.md for the full implementation guide.
Output
${CLAUDESKILLDIR}/src/middleware/response-validator.js - Response schema validation middleware
${CLAUDESKILLDIR}/src/validators/ - Compiled JSON Schema validators per endpoint
${CLAUDESKILLDIR}/tests/contract/ - Contract test suite validating all endpoint responses
${CLAUDESKILLDIR}/reports/val