A Swagger validator and router for Express.js
npm install --save-dev @jdes/swagger-express-validator
Start the server.
- type:
express.App
- required:
false
- default:
express()
Example:
const app = express();
const options = {
app: app
};
- 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();
}
}
}
};
- type:
winston.Logger
- required:
false
- default:
winston
Example:
const options = {
logger: winston
};
- type:
string
- required:
false
- default:
options.logger.level
||info
Useful if you don't pass a logger.
Example:
const options = {
logLevel: 'debug'
};
- type:
Object.<string>
- required:
true
Example:
const options = {
paths: {
controllers: path.join(__dirname, "controller"),
swagger: path.join(__dirname, "swagger", "swagger.json")
}
};
Parent class of the validator's errors.
A sample is available to help ou to write your own app with swagger-express-validator.
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);
});