Chai OpenAPI Response Validator is a testing utility that extends the Chai assertion library, enabling developers to validate HTTP responses and arbitrary JavaScript objects against an OpenAPI (Swagger) specification. The current stable version is 0.14.2, released in January 2022. While it doesn't adhere to a strict release cadence, the project shows active maintenance with several releases in late 2021 and early 2022. Key differentiators include its seamless integration with Chai's assertion syntax (e.g., `expect(response).to.satisfyApiSpec`), support for both OpenAPI 2 and 3 specifications in YAML or JSON formats, and robust handling of `$ref` definitions. It is compatible with various HTTP clients like Axios, `request-promise`, Supertest, Superagent, and `chai-http`, making it versatile for different testing setups, including Mocha. It also provides immediate feedback if the loaded OpenAPI specification itself is invalid.
npm install chai-openapi-response-validatorVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up Chai with `chai-openapi-response-validator`, load a simple OpenAPI specification directly as an object, and then assert that a mock HTTP response (or an actual one from Axios) satisfies the defined API specification.
Review your tests and update any assertions that validate specific error message strings. Focus on the nature of the validation failure rather than verbatim messages.
Always access the default export using `.default` for CommonJS: `const chaiResponseValidator = require('chai-openapi-response-validator').default;`Verify the provided OpenAPI specification is valid using a tool like Swagger Editor. If using a filepath, ensure it's absolute and points to an existing, readable file.
Change `const chaiResponseValidator = require('chai-openapi-response-validator');` to `const chaiResponseValidator = require('chai-openapi-response-validator').default;`Review the detailed error messages provided by the assertion failure to identify the specific validation discrepancy. Either update your API response to match the spec or update the OpenAPI spec to reflect the correct response structure.
Ensure the path and HTTP method in your test precisely match a defined path and method in your loaded OpenAPI specification. Verify the OpenAPI spec is correctly structured and accessible.