Gathering detailed insights and metrics for chai-openapi-response-validator
Gathering detailed insights and metrics for chai-openapi-response-validator
Gathering detailed insights and metrics for chai-openapi-response-validator
Gathering detailed insights and metrics for chai-openapi-response-validator
Use Jest or Chai to assert that HTTP responses satisfy an OpenAPI spec
npm install chai-openapi-response-validator
Typescript
Module System
Node Version
NPM Version
TypeScript (99.98%)
JavaScript (0.02%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
193 Stars
225 Commits
38 Forks
10 Watchers
18 Branches
13 Contributors
Updated on Jul 05, 2025
Latest Version
0.14.2
Package Id
chai-openapi-response-validator@0.14.2
Unpacked Size
30.88 kB
Size
8.23 kB
File Count
19
NPM Version
lerna/4.0.0/node@v14.16.0+x64 (darwin)
Node Version
14.16.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Use Chai to assert that HTTP responses satisfy an OpenAPI spec.
If your server's behaviour doesn't match your API documentation, then you need to correct your server, your documentation, or both. The sooner you know the better.
This plugin lets you automatically test whether your server's behaviour and documentation match. It extends the Chai Assertion Library to support the OpenAPI standard for documenting REST APIs. In your JavaScript tests, you can simply assert expect(responseObject).to.satisfyApiSpec
Features:
$ref
in response definitions (i.e. $ref: '#/definitions/ComponentType/ComponentName'
)axios
, request-promise
, supertest
, superagent
, and chai-http
If you've come here to help contribute - thanks! Take a look at the contributing docs to get started.
1npm install --save-dev chai-openapi-response-validator
1yarn add --dev chai-openapi-response-validator
ES6 / TypeScript
1import chaiResponseValidator from 'chai-openapi-response-validator';
CommonJS / JavaScript
1const chaiResponseValidator = require('chai-openapi-response-validator').default;
1// Set up Chai 2import chai from 'chai'; 3const expect = chai.expect; 4 5// Import this plugin 6import chaiResponseValidator from 'chai-openapi-response-validator'; 7 8// Load an OpenAPI file (YAML or JSON) into this plugin 9chai.use(chaiResponseValidator('path/to/openapi.yml')); 10 11// Write your test (e.g. using Mocha) 12describe('GET /example/endpoint', () => { 13 it('should satisfy OpenAPI spec', async () => { 14 // Get an HTTP response from your server (e.g. using axios) 15 const res = await axios.get('http://localhost:3000/example/endpoint'); 16 17 expect(res.status).to.equal(200); 18 19 // Assert that the HTTP response satisfies the OpenAPI spec 20 expect(res).to.satisfyApiSpec; 21 }); 22});
path/to/openapi.yml
):1openapi: 3.0.0 2info: 3 title: Example API 4 version: 1.0.0 5paths: 6 /example: 7 get: 8 responses: 9 200: 10 description: Response body should be an object with fields 'stringProperty' and 'integerProperty' 11 content: 12 application/json: 13 schema: 14 type: object 15 required: 16 - stringProperty 17 - integerProperty 18 properties: 19 stringProperty: 20 type: string 21 integerProperty: 22 type: integer
openapi.yml
:1// Response includes: 2{ 3 status: 200, 4 body: { 5 stringProperty: 'string', 6 integerProperty: 123, 7 }, 8};
1// Response includes: 2{ 3 status: 200, 4 body: { 5 stringProperty: 'string', 6 integerProperty: 'invalid (should be an integer)', 7 }, 8};
1AssertionError: expected res to satisfy API spec 2 3expected res to satisfy the '200' response defined for endpoint 'GET /example/endpoint' in your API spec 4res did not satisfy it because: integerProperty should be integer 5 6res contained: { 7 body: { 8 stringProperty: 'string', 9 integerProperty: 'invalid (should be an integer)' 10 } 11 } 12} 13 14The '200' response defined for endpoint 'GET /example/endpoint' in API spec: { 15 '200': { 16 description: 'Response body should be a string', 17 content: { 18 'application/json': { 19 schema: { 20 type: 'string' 21 } 22 } 23 } 24 }, 25}
1// Set up Chai 2import chai from 'chai'; 3const expect = chai.expect; 4 5// Import this plugin and the function you want to test 6import chaiResponseValidator from 'chai-openapi-response-validator'; 7import { functionToTest } from 'path/to/your/code'; 8 9// Load an OpenAPI file (YAML or JSON) into this plugin 10chai.use(chaiResponseValidator('path/to/openapi.yml')); 11 12// Write your test (e.g. using Mocha) 13describe('functionToTest()', () => { 14 it('should satisfy OpenAPI spec', async () => { 15 // Assert that the function returns a value satisfying a schema defined in your OpenAPI spec 16 expect(functionToTest()).to.satisfySchemaInApiSpec('ExampleSchemaObject'); 17 }); 18});
path/to/openapi.yml
):1openapi: 3.0.0 2info: 3 title: Example API 4 version: 1.0.0 5paths: 6 /example: 7 get: 8 responses: 9 200: 10 description: Response body should be an ExampleSchemaObject 11 content: 12 application/json: 13 schema: '#/components/schemas/ExampleSchemaObject' 14components: 15 schemas: 16 ExampleSchemaObject: 17 type: object 18 required: 19 - stringProperty 20 - integerProperty 21 properties: 22 stringProperty: 23 type: string 24 integerProperty: 25 type: integer
ExampleSchemaObject
:1// object includes: 2{ 3 stringProperty: 'string', 4 integerProperty: 123, 5};
ExampleSchemaObject
:1// object includes: 2{ 3 stringProperty: 123, 4 integerProperty: 123, 5};
1AssertionError: expected object to satisfy schema 'ExampleSchemaObject' defined in API spec: 2object did not satisfy it because: stringProperty should be string 3 4object was: { 5 { 6 stringProperty: 123, 7 integerProperty: 123 8 } 9 } 10} 11 12The 'ExampleSchemaObject' schema in API spec: { 13 type: 'object', 14 required: [ 15 'stringProperty' 16 'integerProperty' 17 ], 18 properties: { 19 stringProperty: { 20 type: 'string' 21 }, 22 integerProperty: { 23 type: 'integer' 24 } 25 } 26}
1// Set up Chai 2import chai from 'chai'; 3const expect = chai.expect; 4 5// Import this plugin 6import chaiResponseValidator from 'chai-openapi-response-validator'; 7 8// Get an object representing your OpenAPI spec 9const openApiSpec = { 10 openapi: '3.0.0', 11 info: { 12 title: 'Example API', 13 version: '0.1.0', 14 }, 15 paths: { 16 '/example/endpoint': { 17 get: { 18 responses: { 19 200: { 20 description: 'Response body should be a string', 21 content: { 22 'application/json': { 23 schema: { 24 type: 'string', 25 }, 26 }, 27 }, 28 }, 29 }, 30 }, 31 }, 32 }, 33}; 34 35// Load that OpenAPI object into this plugin 36chai.use(chaiResponseValidator(openApiSpec)); 37 38// Write your test (e.g. using Mocha) 39describe('GET /example/endpoint', () => { 40 it('should satisfy OpenAPI spec', async () => { 41 // Get an HTTP response from your server (e.g. using axios) 42 const res = await axios.get('http://localhost:3000/example/endpoint'); 43 44 expect(res.status).to.equal(200); 45 46 // Assert that the HTTP response satisfies the OpenAPI spec 47 expect(res).to.satisfyApiSpec; 48 }); 49});
1// Set up Chai 2import chai from 'chai'; 3const expect = chai.expect; 4 5// Import this plugin and an HTTP client (e.g. axios) 6import chaiResponseValidator from 'chai-openapi-response-validator'; 7import axios from 'axios'; 8 9// Write your test (e.g. using Mocha) 10describe('GET /example/endpoint', () => { 11 // Load your OpenAPI spec from a web endpoint 12 before(async () => { 13 const response = await axios.get('url/to/openapi/spec'); 14 const openApiSpec = response.data; // e.g. { openapi: '3.0.0', <etc> }; 15 chai.use(chaiResponseValidator(openApiSpec)); 16 }); 17 18 it('should satisfy OpenAPI spec', async () => { 19 // Get an HTTP response from your server (e.g. using axios) 20 const res = await axios.get('http://localhost:3000/example/endpoint'); 21 22 expect(res.status).to.equal(200); 23 24 // Assert that the HTTP response satisfies the OpenAPI spec 25 expect(res).to.satisfyApiSpec; 26 }); 27});
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/21 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
63 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More