How to use express-openapi-validator - 1 common examples

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

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

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 2 months ago

Package Health Score

89 / 100
Full package analysis

Popular express-openapi-validator functions