Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Dec 26, 2020
2 parents 3347f5d + e08f45a commit fed1a58
Show file tree
Hide file tree
Showing 21 changed files with 7,583 additions and 367 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,15 @@
"test",
"ideas"
]
},
{
"login": "electrotype",
"name": "Electro Type",
"avatar_url": "https://avatars2.githubusercontent.com/u/1643012?v=4",
"profile": "https://www.spincast.org",
"contributions": [
"ideas"
]
}
],
"contributorsPerLine": 7,
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,13 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
</tr>
<tr>
<td align="center"><a href="https://www.linkedin.com/in/volodymyr-kolesnykov"><img src="https://avatars1.githubusercontent.com/u/7810770?v=4" width="100px;" alt=""/><br /><sub><b>Volodymyr Kolesnykov</b></sub></a><br /><a href="https://github.com/cdimascio/express-openapi-validator/commits?author=sjinks" title="Code">💻</a> <a href="https://github.com/cdimascio/express-openapi-validator/commits?author=sjinks" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/pilerou"><img src="https://avatars2.githubusercontent.com/u/664865?v=4" width="100px;" alt=""/><br /><sub><b>Pierre Le Roux</b></sub></a><br /><a href="https://github.com/cdimascio/express-openapi-validator/commits?author=pilerou" title="Code">💻</a> <a href="https://github.com/cdimascio/express-openapi-validator/commits?author=pilerou" title="Tests">⚠️</a> <a href="#ideas-pilerou" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https://www.spincast.org"><img src="https://avatars2.githubusercontent.com/u/1643012?v=4" width="100px;" alt=""/><br /><sub><b>Electro Type</b></sub></a><br /><a href="#ideas-electrotype" title="Ideas, Planning, & Feedback">🤔</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
Expand All @@ -1219,4 +1220,4 @@ We plan to publicize a variety of links here.

[MIT](LICENSE)

<a href="https://www.buymeacoffee.com/m97tA5c" target="_blank"><img src="https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
<a href="https://www.buymeacoffee.com/m97tA5c" target="_blank"><img src="https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
2 changes: 1 addition & 1 deletion assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ See the [full documentation](https://github.com/cdimascio/express-openapi-valida

## License

MIT
MIT
27 changes: 27 additions & 0 deletions examples/7-response-date-serialization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# example

## Install

```shell
npm run deps && npm i
```

## Run

From this `7-schema-object-mapper` directory, run:

```shell
npm start
```

## Try

```shell

## call pets
## the call below should return 400 since it requires additional parameters
curl http://localhost:3000/v1/pets?type=dog&limit=3

## Get the first item id
curl http://localhost:3000/v1/pets/<first item id>
```
50 changes: 50 additions & 0 deletions examples/7-response-date-serialization/api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore schemaObjectMapper variant
description: A sample API
termsOfService: http://swagger.io/terms/
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
- url: /v1
paths:
/date-time:
get:
responses:
200:
description: date-time handler
content:
application/json:
schema:
type: object
properties:
created_at:
type: string
format: date-time
id:
type: number
/date:
get:
responses:
200:
description: date handler
content:
application/json:
schema:
$ref: '#/components/schemas/User'

components:
schemas:
Date:
type: string
format: date
User:
type: object
properties:
id:
type: number
created_at:
$ref: "#/components/schemas/Date"

57 changes: 57 additions & 0 deletions examples/7-response-date-serialization/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const http = require('http');
const OpenApiValidator = require('express-openapi-validator');

const port = 3000;
const app = express();
const apiSpec = path.join(__dirname, 'api.yaml');

// 1. Install bodyParsers for the request types your API will support
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.text());
app.use(bodyParser.json());

// Optionally serve the API spec
app.use('/spec', express.static(apiSpec));

// 2. Install the OpenApiValidator on your express app
app.use(
OpenApiValidator.middleware({
apiSpec,
validateResponses: true,
}),
);
// 3. Add routes
app.get('/v1/ping', function (req, res, next) {
res.send('pong');
});

app.get('/v1/date-time', function (req, res, next) {
res.json({
id: 1,
created_at: new Date(),
});
});

app.get('/v1/date', function (req, res, next) {
res.json({
id: 1,
created_at: new Date(),
});
});

// 4. Create a custom error handler
app.use((err, req, res, next) => {
// format errors
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});

http.createServer(app).listen(port);
console.log(`Listening on port ${port}`);

module.exports = app;

0 comments on commit fed1a58

Please sign in to comment.