How to use the express-openapi-validator.OpenApiValidator function in express-openapi-validator

To help you get started, we’ve selected a few express-openapi-validator examples, based on popular ways it is used in public projects.

github cdimascio / express-openapi-validator / example / app.js View on Github external
// 1. Install bodyParsers for the request types your API will support
app.use(bodyParser.urlencoded());
app.use(bodyParser.text());
app.use(bodyParser.json());

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

const spec = path.join(__dirname, 'openapi.yaml');
app.use('/spec', express.static(spec));

// 2. Install the OpenApiValidator on your express app
new OpenApiValidator({
  apiSpec: './example.yaml',
  validateResponses: true,
  // securityHandlers: {
  //   ApiKeyAuth: (req, scopes, schema) => true,
  // },
}).install(app);

let id = 3;
// 3. Add routes
app.get('/v1/pets', function(req, res, next) {
  res.json(pets);
});

app.post('/v1/pets', function(req, res, next) {
  res.json({ id: id++, ...req.body });
});

express-openapi-validator

Automatically validate API requests and responses with OpenAPI 3 and Express.

MIT
Latest version published 4 months ago

Package Health Score

81 / 100
Full package analysis

Popular express-openapi-validator functions

Similar packages