diff --git a/README.md b/README.md index e7d5d4a..a349c44 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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