rest swagger validator
rest-swagger-validator is a module for use with one or more swagger definitions in order to validate that a request and/or response are valid.
the need
Swagger api specs creation/modification was added as the first step in any agile story. As part of our integration tests, we wanted to validate that any recorded api calls were valid responses to record and any actual api calls were valid requests.
our solution
Usage of the module could look something like this:
const swagga = require('rest-swagger-validator');
const swaggerFilePath = 'my-swagger.json';
var swag;
const recordGet = (url, responseStatus, responseBody) => {
const validationResult = swag.validateResponse(url, 'GET', responseStatus, responseBody);
expect(validationResult).toBeUndefined();
}
const validateGet = (url) => {
const lastGetCall = myCallHistory.getLastGetCall(url);
const validationResult = swag.validateRequest(url, 'GET', {
query: lastGetCall.queryParameters,
body: lastGetCall.body,
});
expect(validationResult).toBeUndefined();
}
beforeAll(async () => {
swag = await swagga.createFor(swaggerFilePath);
})
it('...', async () => {
await recordGet('/some/url', 200, {
field1: 'val1',
field2: 'val2',
});
await validateGet('/some/url');
})