Socket
Socket
Sign inDemoInstall

express-openapi-validator

Package Overview
Dependencies
Maintainers
0
Versions
278
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-openapi-validator

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


Version published
Weekly downloads
263K
increased by3.32%
Maintainers
0
Weekly downloads
ย 
Created

What is express-openapi-validator?

express-openapi-validator is a middleware for Express.js that validates API requests and responses against an OpenAPI 3.0 specification. It helps ensure that your API adheres to the defined contract, improving reliability and maintainability.

What are express-openapi-validator's main functionalities?

Request Validation

This feature validates incoming requests against the OpenAPI specification. If a request does not conform to the spec, an error is returned.

const express = require('express');
const OpenApiValidator = require('express-openapi-validator');
const app = express();

app.use(express.json());

app.use(
  OpenApiValidator.middleware({
    apiSpec: './api.yaml',
    validateRequests: true,
  })
);

app.post('/pets', (req, res) => {
  res.json({ message: 'Pet added successfully' });
});

app.use((err, req, res, next) => {
  res.status(err.status || 500).json({ message: err.message });
});

app.listen(3000, () => console.log('Server running on port 3000'));

Response Validation

This feature validates outgoing responses against the OpenAPI specification. If a response does not conform to the spec, an error is returned.

const express = require('express');
const OpenApiValidator = require('express-openapi-validator');
const app = express();

app.use(express.json());

app.use(
  OpenApiValidator.middleware({
    apiSpec: './api.yaml',
    validateResponses: true,
  })
);

app.get('/pets', (req, res) => {
  res.json([{ id: 1, name: 'Fluffy' }]);
});

app.use((err, req, res, next) => {
  res.status(err.status || 500).json({ message: err.message });
});

app.listen(3000, () => console.log('Server running on port 3000'));

Security Validation

This feature validates security requirements defined in the OpenAPI specification. It ensures that requests meet the necessary security criteria, such as API keys or OAuth tokens.

const express = require('express');
const OpenApiValidator = require('express-openapi-validator');
const app = express();

app.use(express.json());

app.use(
  OpenApiValidator.middleware({
    apiSpec: './api.yaml',
    validateSecurity: true,
  })
);

app.get('/secure-endpoint', (req, res) => {
  res.json({ message: 'Secure data' });
});

app.use((err, req, res, next) => {
  res.status(err.status || 500).json({ message: err.message });
});

app.listen(3000, () => console.log('Server running on port 3000'));

Other packages similar to express-openapi-validator

Keywords

FAQs

Package last updated on 31 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc