Skip to content

Commit 90927c7

Browse files
authored
Docs/improve validator docs (#228)
* ci: remove npm unnecesary files * docs: update validator example * docs: improve validator readme docs * chore: bump version to 1.7.1 * chore: remove vscode editor options * chore: bump version to 1.8.0 * fix: sonarcloud example code errors * fix: major sonarcloud issues * fix: minor sonarcloud issues
1 parent faaab1e commit 90927c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+10606
-140
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Linux ###
66
*~
7-
7+
.vscode
88
# temporary files which can be created if a process still has a handle open of a deleted file
99
.fuse_hidden*
1010

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
npm-debug.log
33
examples
44
test
5+
.github

.vscode/launch.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ npm i express-jsdoc-swagger
3737
## Basic Usage
3838

3939
```javascript
40+
// index.js file
4041
const express = require('express');
4142
const expressJSDocSwagger = require('express-jsdoc-swagger');
4243

@@ -54,6 +55,7 @@ const options = {
5455
scheme: 'basic',
5556
},
5657
},
58+
// Base directory which we use to locate your JSDOC files
5759
baseDir: __dirname,
5860
// Glob pattern to find your jsdoc files (multiple patterns can be added in an array)
5961
filesPattern: './**/*.js',
@@ -94,7 +96,7 @@ app.listen(PORT, () => console.log(`Example app listening at http://localhost:${
9496

9597
## Basic Examples
9698

97-
1. Basic configuration
99+
1. Basic configuration options.
98100

99101
```javascript
100102
const options = {
@@ -129,7 +131,7 @@ const options = {
129131
*/
130132
```
131133

132-
3. Endpoint that returns a `Songs` model array
134+
3. Endpoint which returns a `Songs` model array in the response.
133135

134136
```javascript
135137
/**
@@ -145,6 +147,24 @@ app.get('/api/v1/albums', (req, res) => (
145147
));
146148
```
147149

150+
3. Endpoint PUT with body and path params which returns a `Songs` model array in the response.
151+
152+
```javascript
153+
/**
154+
* PUT /api/v1/albums/{id}
155+
* @summary Update album
156+
* @tags album
157+
* @param {string} name.path - name param description
158+
* @param {Song} request.body.required - songs info
159+
* @return {array<Song>} 200 - success response - application/json
160+
*/
161+
app.put('/api/v1/albums/:id', (req, res) => (
162+
res.json([{
163+
title: 'abum 1',
164+
}])
165+
));
166+
```
167+
148168
4. Basic endpoint definition with tags, params and basic authentication
149169

150170
```javascript
@@ -203,23 +223,34 @@ Install using the node package registry:
203223
npm install --save express-oas-validator
204224
```
205225

206-
After this you have to initialize using the `finish` event. More info in this [sections](eventEmitter.md).
226+
We have to wait until we have the full swagger schema to initiate the validator.
207227

208228
```js
209-
const instance = expressJSDocSwagger(app)(options);
229+
// validator.js
230+
const { init } = require('express-oas-validator');
210231

211-
instance.on('finish', data => {
212-
init(data);
213-
resolve(app);
232+
const validators = instance => new Promise((resolve, reject) => {
233+
instance.on('finish', (swaggerDef) => {
234+
const { validateRequest, validateResponse } = init(swaggerDef);
235+
resolve({ validateRequest, validateResponse });
236+
});
237+
238+
instance.on('error', (error) => {
239+
reject(error);
240+
});
214241
});
242+
243+
module.exports = validators;
244+
215245
```
216246

217-
This is a full example of how it works.
247+
You can check out this also in our [example folder](https://github.com/BRIKEV/express-jsdoc-swagger/tree/master/examples/validator).
218248

219249
```js
250+
// index.js
220251
const express = require('express');
221252
const expressJSDocSwagger = require('express-jsdoc-swagger');
222-
const { init, validateRequest, validateResponse } = require('express-oas-validator');
253+
const validator = require('./validator');
223254

224255
const options = {
225256
info: {
@@ -236,15 +267,10 @@ const options = {
236267
const app = express();
237268
const instance = expressJSDocSwagger(app)(options);
238269

239-
const serverApp = () => new Promise(resolve => {
240-
instance.on('finish', data => {
241-
init(data);
242-
resolve(app);
243-
});
244-
270+
const serverApp = async () => {
271+
const { validateRequest, validateResponse } = await validator(instance);
245272
app.use(express.urlencoded({ extended: true }));
246273
app.use(express.json());
247-
248274
/**
249275
* A song
250276
* @typedef {object} Song
@@ -292,9 +318,21 @@ const serverApp = () => new Promise(resolve => {
292318
app.use((err, req, res, next) => {
293319
res.status(err.status).json(err);
294320
});
295-
});
296321

297-
module.exports = serverApp;
322+
return app;
323+
};
324+
325+
const PORT = process.env.PORT || 4000;
326+
327+
serverApp()
328+
.then(app =>
329+
app.listen(PORT, () =>
330+
console.log(`Listening PORT: ${PORT}`)
331+
))
332+
.catch((err) => {
333+
console.error(err);
334+
process.exit(1);
335+
});
298336
```
299337

300338
You can visit our [documentation](https://brikev.github.io/express-jsdoc-swagger-docs/#/validator).

consumers/jsdocInfo.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const doctrine = require('doctrine');
22

33
const jsdocInfo = (options = { unwrap: true }) => comments => {
44
if (!comments || !Array.isArray(comments)) return [];
5-
const parsed = comments.map(comment => {
5+
return comments.map(comment => {
66
const parsedValue = doctrine.parse(comment, options);
77
const tags = parsedValue.tags.map(tag => ({
88
...tag,
@@ -15,7 +15,6 @@ const jsdocInfo = (options = { unwrap: true }) => comments => {
1515
description,
1616
};
1717
});
18-
return parsed;
1918
};
2019

2120
module.exports = jsdocInfo;

examples/combineSchemas/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ expressJSDocSwagger(app)(options);
4343
* @param {number} id.path - song id
4444
* @return {oneOf|IntrumentalSong|PopSong} 200 - success response - application/json
4545
*/
46-
app.get('/api/v1/song/:id', (req, res) => (
46+
app.get('/api/v1/song/:id', (_req, res) => (
4747
res.json({
4848
title: 'abum 1',
4949
})

examples/configuration/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ expressJSDocSwagger(app)(options);
5757
* @summary This is the summary of the endpoint
5858
* @return {string} 200 - success response
5959
*/
60-
app.get('/api/v1', (req, res) => res.send('Hello World!'));
60+
app.get('/api/v1', (_req, res) => res.send('Hello World!'));
6161

6262
app.listen(port, () => logger.info(`Example app listening at http://localhost:${port}`));

examples/configuration/multipleFiles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ expressJSDocSwagger(app)(options);
5757
* @summary This is the summary of the endpoint
5858
* @return {string} 200 - success response
5959
*/
60-
app.get('/api/v1', (req, res) => res.send('Hello World!'));
60+
app.get('/api/v1', (_req, res) => res.send('Hello World!'));
6161

6262
app.listen(port, () => logger.info(`Example app listening at http://localhost:${port}`));

examples/configuration/swaggerOptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ expressJSDocSwagger(app)(options);
5151
* @summary This is the summary of the endpoint
5252
* @return {array<Song>} 200 - success response - application/json
5353
*/
54-
app.get('/api/v1/albums', (req, res) => res.json([]));
54+
app.get('/api/v1/albums', (_req, res) => res.json([]));
5555

5656
app.listen(port, () => logger.info(`Example app listening at http://localhost:${port}`));

examples/merge/simple.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ expressJSDocSwagger(app)(options, userSwagger);
3535
* @param {array<object>} name.query.required - name param description
3636
* @return {array<object>} 200 - success response - application/json
3737
*/
38-
app.get('/api/v1/albums', (req, res) => (
38+
app.get('/api/v1/albums', (_req, res) => (
3939
res.json([{
4040
title: 'abum 1',
4141
}])

examples/nullableFields/explicitNullable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ expressJSDocSwagger(app)(options);
3737
* @param {Song} request.body.required - Song to add
3838
* @return {string} 200 - Success message
3939
*/
40-
app.post('/api/v1/songs', (req, res) => res.send('You saved a song!'));
40+
app.post('/api/v1/songs', (_req, res) => res.send('You saved a song!'));
4141

4242
/**
4343
* GET /api/v1/song
4444
* @summary Nullable props in response
4545
* @return {array<Song>} 200 - List of songs
4646
*/
47-
app.get('/api/v1/song', (req, res) => res.json([
47+
app.get('/api/v1/song', (_req, res) => res.json([
4848
{
4949
id: 1,
5050
title: 'Song from album',

examples/nullableFields/nullableByDefault.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ expressJSDocSwagger(app)(options);
3636
* @param {Song} request.body.required - Song to add
3737
* @return {string} 200 - Success message
3838
*/
39-
app.post('/api/v1/songs', (req, res) => res.send('You saved a song!'));
39+
app.post('/api/v1/songs', (_req, res) => res.send('You saved a song!'));
4040

4141
app.listen(port, () => logger.info(`Example app listening at http://localhost:${port}`));

0 commit comments

Comments
 (0)