-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# 🦋 express-openapi-validator | ||
|
||
**An OpenApi validator for ExpressJS** that automatically validates **API** _**requests**_ and _**responses**_ using an **OpenAPI 3** specification. | ||
|
||
[🦋express-openapi-validator](https://github.com/cdimascio/express-openapi-validator) is an unopinionated library that integrates with new and existing API applications. express-openapi-validator lets you write code the way you want; it does not impose any coding convention or project layout. Simply, install the validator onto your express app, point it to your OpenAPI 3 specification, then define and implement routes the way you prefer. See an [example](#example-express-api-server). | ||
|
||
**Features:** | ||
|
||
- ✔️ request validation | ||
- ✔️ response validation (json only) | ||
- 👮 security validation / custom security functions | ||
- 👽 3rd party / custom formats | ||
- 🧵 optionally auto-map OpenAPI endpoints to Express handler functions | ||
- ✂️ **\$ref** support; split specs over multiple files | ||
- 🎈 file upload | ||
|
||
## Install | ||
|
||
```shell | ||
npm install express-openapi-validator | ||
``` | ||
|
||
## Usage | ||
|
||
1. Require/import the openapi validator | ||
|
||
```javascript | ||
const OpenApiValidator = require('express-openapi-validator'); | ||
``` | ||
|
||
2. Install the middleware | ||
|
||
```javascript | ||
app.use( | ||
OpenApiValidator.middleware({ | ||
apiSpec: './openapi.yaml', | ||
validateRequests: true, // (default) | ||
validateResponses: true, // false by default | ||
}), | ||
); | ||
``` | ||
|
||
3. Register an error handler | ||
|
||
```javascript | ||
app.use((err, req, res, next) => { | ||
// format error | ||
res.status(err.status || 500).json({ | ||
message: err.message, | ||
errors: err.errors, | ||
}); | ||
}); | ||
``` | ||
|
||
_**Important:** Ensure express is configured with all relevant body parsers. Body parser middleware functions must be specified prior to any validated routes. See an [example](#example-express-api-server)_. |