Swagger Express Validator
A Swagger validator and router for Express.js
Table of contents
Setup
npm install --save-dev @jdes/swagger-express-validator
API
App.start(options: Object): Promise.<express.App>
Start the server.
Options
app
An Express' app created with express()
- type:
express.App
- required:
false
- default:
express()
Example:
const app = express();
const options = {
app: app
};
hooks
A list of middleware' hooks
- type:
Object.<{before: Function, after: Function}>
- required:
false
- default:
{}
A list of hooks for every used middlewares:
- swaggerRouter: Get the right method for the current route
- requestHandler: Check parameters, and default response
- requestPerformer: Execute the controller's method with the
req
- responseValidator: Validate the
req.response
with the associated schema - responseSender: Get the
req.response
object and send it - errorHandler: Error middleware
Example:
const options = {
hooks: {
requestHandler: {
before: (req, res, next) => {
console.log("Hello World!");
next();
}
},
errorHandler: {
after: (req, res, next) => {
console.error("Arrrghhh");
next();
}
}
}
};
logger
A Logger
- type:
winston.Logger
- required:
false
- default:
winston
Example:
const options = {
logger: winston
};
logLevel
The logger's log level
- type:
string
- required:
false
- default:
options.logger.level
||info
Useful if you don't pass a logger.
Example:
const options = {
logLevel: 'debug'
};
paths
The required paths
- type:
Object.<string>
- required:
true
Example:
const options = {
paths: {
controllers: path.join(__dirname, "controller"),
swagger: path.join(__dirname, "swagger", "swagger.json")
}
};
HttpError: Error
Parent class of the validator's errors.
Sample
A sample is available to help ou to write your own app with swagger-express-validator.
Example
const App = require("@jdes/swagger-express-validator").App;
const path = require("path");
const logger = require("winston");
const options = {
logger,
paths: {
controllers: path.join(__dirname, "controller"),
swagger: path.join(__dirname, "swagger", "swagger.json")
}
};
App.start(options)
.then((app) => {
app.listen(8080, () => {
logger.info(`Server launched on 8080`);
});
})
.catch((error) => {
logger.critical(error);
});