Skip to content

Commit

Permalink
Merge pull request #63 from codeclown/match-docs
Browse files Browse the repository at this point in the history
Add documentation for match()
  • Loading branch information
Hilzu committed Jan 31, 2020
2 parents e0d7377 + 45a7b0d commit b330e2a
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ const server = app.listen(3000, () => {
});
```

Or using `match()`:

```javascript
// Apply to all requests
app.use(validator.match());

// On each request validator.validate("post", "/echo") is called automatically,
// if it has a matching specification in the OpenAPI schema
app.post("/echo", (req, res, next) => {
res.json({ output: req.body.input });
});
```

### `openapi.yaml`

```yaml
Expand Down Expand Up @@ -215,6 +228,34 @@ Object][openapi-path-item-object]:
`RequestHandler` is an express middleware function with the signature
`(req: Request, res: Response, next: NextFunction): any;`.

#### `match(): RequestHandler`

Returns an express middleware function which calls `validate()` based on the
request method and path. Using this function removes the need to specify
`validate()` middleware for each express endpoint individually.

Note that behaviour is different to `validate()` for routes where no schema is
specified: `validate()` will throw an exception if no matching route
specification is found in the OpenAPI schema. `match()` will not throw an
exception in this case; the request is simply not validated. Be careful to
ensure you OpenAPI schema contains validators for each endpoint if using
`match()`.

The following examples achieve the same result:

```javascript
// Using validate()
app.post("/echo", validator.validate("post", "/echo"), (req, res, next) => {
res.json({ output: req.body.input });
});

// Using match()
app.use(validator.match());
app.post("/echo", (req, res, next) => {
res.json({ output: req.body.input });
});
```

#### `validateResponse(method: Operation, path: string): (res: any) => void`

Creates a function for the given operation that can be used to validate
Expand Down

0 comments on commit b330e2a

Please sign in to comment.