Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tinacious committed Jul 2, 2020
1 parent d2793fc commit 7f21a17
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,21 @@

This custom middleware is for those who prefer to use Express but want to use the Joi validation library to validate the query, params, and body of requests.


## Usage

Install with NPM:

```
npm install express-joi-validate --save
```

Or with Yarn:

```
yarn add express-joi-validate
```

Without route validation, an endpoint may look something like this:

```js
Expand All @@ -28,7 +41,24 @@ const contactSchema = {
app.get('/contacts/:id', validate(contactSchema), contactsHandler);
```

For detailed usage and more examples, see the blog post [Express route param validation (Node.js)](https://tinaciousdesign.com/blog/express-route-param-validation-nodejs).
In TypeScript, it would look like this:

```typescript
import Joi, { SchemaMap } from '@hapi/joi';
import validate from 'express-joi-validate';

const contactSchema: SchemaMap = {
params: {
id: Joi.number().required()
}
};

app.get('/contacts/:id', validate(contactSchema), contactsHandler);
```

Note: for the above, the type `SchemaMap` is inferred and not required.

For more information about this library, see the blog post [Express route param validation (Node.js)](https://tinaciousdesign.com/blog/express-route-param-validation-nodejs).

## Development

Expand Down

0 comments on commit 7f21a17

Please sign in to comment.