Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Mar 21, 2019
2 parents 2327347 + 041d197 commit 1b11c97
Showing 1 changed file with 86 additions and 3 deletions.
89 changes: 86 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

ExpressJs middleware that automatically validates API requests using an OpenAPI 3.0 specification,

Try out the [sample project](https://github.com/cdimascio/express-middleware-openapi-example)

## Install

Try this pre-release alpha version:
Expand All @@ -14,8 +16,6 @@ npm i express-middleware-openapi

## Usage

see [app.js](example/app.js) for a complete example.

### Basic

```javascript
Expand Down Expand Up @@ -51,7 +51,7 @@ new OpenApiMiddleware({
}).install(app);
```

## Example
## Example API Server

Try the complete example below:

Expand Down Expand Up @@ -100,3 +100,86 @@ console.log('Listening on port 3000');

module.exports = app;
```

## [Example API Server (Full Project Source)](https://github.com/cdimascio/express-middleware-openapi-example)

A full working example lives [here](https://github.com/cdimascio/express-middleware-openapi-example)

## Example validation responses

#### Validate a query parameter with a value constraint

```shell
curl http://localhost:3000/v1/pets/as |jq
{
"errors": [
{
"path": "id",
"errorCode": "type.openapi.validation",
"message": "should be integer",
"location": "path"
}
]
}
```

#### Validate a query parameter with a range constraint

```shell
curl http://localhost:3000/v1/pets?limit=1 |jq
{
"errors": [
{
"path": "limit",
"errorCode": "minimum.openapi.validation",
"message": "should be >= 5",
"location": "query"
},
{
"path": "test",
"errorCode": "required.openapi.validation",
"message": "should have required property 'test'",
"location": "query"
}
]
}
```

#### Validate the query parameter's value type

```shell
curl --request POST \
--url http://localhost:3000/v1/pets \
--header 'content-type: application/xml' \
--data '{
"name": "test"
}' |jq
{
"errors": [
{
"message": "Unsupported Content-Type application/xml"
}
]
}
```

#### Validate a POST body to ensure required parameters are present

```shell
λ my-test curl --request POST \
--url http://localhost:3000/v1/pets \
--header 'content-type: application/json' \
--data '{
}'|jq
"errors": [
{
"path": "name",
"errorCode": "required.openapi.validation",
"message": "should have required property 'name'",
"location": "body"
}
]
}
```

#### ...and much more. Try it out!

0 comments on commit 1b11c97

Please sign in to comment.