swagger-endpoint-validator
Advanced tools
Comparing version 1.1.1 to 1.1.2
87
index.js
@@ -11,3 +11,18 @@ const expressSwaggerGenerator = require('express-swagger-generator'); | ||
let singleton = null; | ||
let paths = null; | ||
/** | ||
* Replaces :<string> by {string}. | ||
* @param {string} pathStr - string to replace. | ||
* @returns Replaced string. | ||
*/ | ||
const formatPath = pathStr => pathStr.replace(/{/g, ':').replace(/}/g, ''); | ||
/** | ||
* Creates the validator singleton instance. | ||
* @param {object} app - Express application object. | ||
* @param {object} swaggerOptions - Swagger options. | ||
* @param {string} format - One of: 'jsdoc' or 'yaml'. | ||
* @returns Validator singleton instance initialized. | ||
*/ | ||
const createInstance = (app, swaggerOptions, format) => { | ||
@@ -55,2 +70,9 @@ const instance = { | ||
/** | ||
* Validates the payload for a specific endpoint. | ||
* @param {object} payload - Payload to be validated. | ||
* @param {object} request - Express request object. | ||
* @param {boolean} isInput - true if it's an input payload for the endpoint, false otherwise. | ||
* @throws {swaggerValidatorError} Swagger validator error. | ||
*/ | ||
const validate = (payload, request, isInput) => { | ||
@@ -67,8 +89,2 @@ if (!singleton) { | ||
const { path: routePath } = request.route; | ||
const formatPath = pathStr => pathStr.replace(/{/g, ':').replace(/}/g, ''); // Replace :<string> by {string} in path | ||
const paths = Object.keys(singleton.paths).reduce((acum, item) => ( | ||
{ ...acum, [formatPath(item)]: singleton.paths[item] } | ||
), {}); | ||
const path = paths[routePath]; | ||
@@ -99,17 +115,48 @@ | ||
module.exports = { | ||
init: (app, swaggerOptions, format = 'jsdoc') => { | ||
if (!['jsdoc', 'yaml'].includes(format)) { | ||
throw swaggerValidatorError(`${format} format not supported`); | ||
} | ||
/** | ||
* Initializes the validator. | ||
* @param {*} app - Express application object. | ||
* @param {*} swaggerOptions - Swagger options. | ||
* @param {*} format - One of: 'jsdoc' or 'yaml'. | ||
*/ | ||
const init = (app, swaggerOptions, format = 'jsdoc') => { | ||
if (!['jsdoc', 'yaml'].includes(format)) { | ||
throw swaggerValidatorError(`${format} format not supported`); | ||
} | ||
if (!singleton) { | ||
singleton = createInstance(app, swaggerOptions, format); | ||
} | ||
}, | ||
reset: () => { | ||
singleton = null; | ||
}, | ||
validateAPIInput: (payload, request) => validate(payload, request, true), | ||
validateAPIOutput: (payload, request) => validate(payload, request, false), | ||
if (!singleton) { | ||
singleton = createInstance(app, swaggerOptions, format); | ||
paths = Object.keys(singleton.paths).reduce((acum, item) => ( | ||
{ ...acum, [formatPath(item)]: singleton.paths[item] } | ||
), {}); | ||
} | ||
}; | ||
/** | ||
* Resets the validator. | ||
*/ | ||
const reset = () => { | ||
singleton = null; | ||
}; | ||
/** | ||
* Validates a payload used as input body to a REST endpoint. | ||
* @param {object} payload - Payload to be validated. | ||
* @param {object} request - Express request object details. | ||
*/ | ||
const validateAPIInput = (payload, request) => validate(payload, request, true); | ||
/** | ||
* Validates a payload returned by a REST endpoint. | ||
* @param {object} payload - Payload to be validated. | ||
* @param {object} request - Express request object details. | ||
*/ | ||
const validateAPIOutput = (payload, request) => validate(payload, request, false); | ||
module.exports = { | ||
init, | ||
reset, | ||
validateAPIInput, | ||
validateAPIOutput, | ||
}; |
{ | ||
"name": "swagger-endpoint-validator", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "A validator of API endpoints to check that input and output match with the swagger specification for the API", | ||
@@ -26,15 +26,15 @@ "main": "index.js", | ||
"dependencies": { | ||
"express-swagger-generator": "^1.1.15", | ||
"express-swagger-generator": "^1.1.17", | ||
"swagger-jsdoc": "^3.5.0", | ||
"swagger-model-validator": "^3.0.12", | ||
"swagger-ui-express": "^4.1.2" | ||
"swagger-model-validator": "^3.0.16", | ||
"swagger-ui-express": "^4.1.3" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^6.2.1", | ||
"eslint": "^6.8.0", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-jest": "^22.15.2", | ||
"husky": "^3.0.4", | ||
"eslint-plugin-import": "^2.20.0", | ||
"eslint-plugin-jest": "^23.6.0", | ||
"husky": "^4.0.10", | ||
"jest": "^24.9.0", | ||
"jest-junit": "^9.0.0", | ||
"jest-junit": "^10.0.0", | ||
"mock-express": "^1.3.0" | ||
@@ -41,0 +41,0 @@ }, |
29841
618