chai-openapi-response-validator
Version:
Use Chai to assert that HTTP responses satisfy an OpenAPI spec
28 lines • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
function default_1(chai, openApiSpec) {
const { Assertion, AssertionError } = chai;
Assertion.addMethod('satisfySchemaInApiSpec', function (schemaName) {
const actualObject = this._obj; // eslint-disable-line no-underscore-dangle
const schema = openApiSpec.getSchemaObject(schemaName);
if (!schema) {
// alert users they are misusing this assertion
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new AssertionError('The argument to satisfySchemaInApiSpec must match a schema in your API spec');
}
const validationError = openApiSpec.validateObject(actualObject, schema);
const pass = !validationError;
this.assert(pass, pass
? ''
: getExpectReceivedToSatisfySchemaInApiSpecMsg(actualObject, schemaName, schema, validationError), getExpectReceivedNotToSatisfySchemaInApiSpecMsg(actualObject, schemaName, schema), null);
});
}
exports.default = default_1;
function getExpectReceivedToSatisfySchemaInApiSpecMsg(received, schemaName, schema, validationError) {
return (0, utils_1.joinWithNewLines)(`expected object to satisfy the '${schemaName}' schema defined in your API spec`, `object did not satisfy it because: ${validationError}`, `object was: ${(0, utils_1.stringify)(received)}`, `The '${schemaName}' schema in API spec: ${(0, utils_1.stringify)(schema)}`);
}
function getExpectReceivedNotToSatisfySchemaInApiSpecMsg(received, schemaName, schema) {
return (0, utils_1.joinWithNewLines)(`expected object not to satisfy the '${schemaName}' schema defined in your API spec`, `object was: ${(0, utils_1.stringify)(received)}`, `The '${schemaName}' schema in API spec: ${(0, utils_1.stringify)(schema)}`);
}
//# sourceMappingURL=satisfySchemaInApiSpec.js.map