# @sloang/joi-middleware

> A validation middleware for expressjs.

Latest version **1.0.1** (published 2019-10-20) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @sloang/joi-middleware
pnpm add @sloang/joi-middleware
yarn add @sloang/joi-middleware
bun add @sloang/joi-middleware
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-10-20 |
| First published | 2019-10-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sloan Gwaltney |
| Maintainers | sloang |
| Keywords | joi, validation, express, middleware |

## Links

- npm: https://www.npmjs.com/package/@sloang/joi-middleware
- Repository: https://github.com/sloanbgwaltney/joi-middleware
- Homepage: https://github.com/sloanbgwaltney/joi-middleware#readme
- Issues: https://github.com/sloanbgwaltney/joi-middleware/issues
- npm.io page: https://npm.io/package/@sloang/joi-middleware

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-10-20
- 1.0.0 — 2019-10-19

## README

# @sloang/joi-middleware

> A validation middleware for expressjs using Joi.

![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@sloang/joi-middleware)
![npm (scoped)](https://img.shields.io/npm/v/@sloang/joi-middleware)
![NPM](https://img.shields.io/npm/l/@sloang/joi-middleware)

Remove validation from your controllers and put them into your middleware.

## Usage

```js
const express = require('express')
const app = express()
const joi = require('@hapi/joi')
const joiMiddleware = require('@sloang/joi-middleware')
const joiValidationObject = joi.object({
    title: joi.string().min(5).max(40).required()
})

app.post('/', joiMiddleware(joiValidationObject), (req, res, next) => {
    res.status(200).send('It Works')
})

app.use((err, req, res, next) => {
    if (err.name == 'ValidationError') {
        res.status(400).send('Validation Error')
    }
})

```

outputs

On valid validation
```
It Works
```

On failed validation
```
Validation Error
```

## API

```js
let joiMiddleware = require('@sloang/joi-middleware')
```

`joiMiddlware(validationObject)`


When joiMiddleware is called only with the Joi validationObject it defaults to validating `req.body` you can validate other objects by using `joiMiddleware(validationObject, object)`. If validation fails next is called with the validation error as its argument. Otherwise next is called with no argument

`joiMiddleware(validationObject, object)`

When using this method you are providing the middleware both the Joi validation object and the object to be validated. If validation fails next is called with the validation error as its argument. Otherwise next is called with no argument. This method can be useful when trying to validate other request object keys. For example 
```js
const router = require('express').Router()
let joiMiddleware = require('@sloang/joi-middleware')

router.post('/test', 
(req, res, next) => 
    joiMiddleware(userValidator, req.user)(req, res, next))
``` 

### Important Note

Joi is technically not specifically required for this middleware to work. Any validation package that has a `.validate` method that returns an object with an error key (`{error}`) when the object is invalid should in theory work for this middleware, however, Joi is used in test and should always be functional with this package.

## Install

With [npm](https://npmjs.org/) installed, run

```
$ npm install @sloang/joi-middleware @hapi/joi
```

## See Also

- [`@hapi/joi`](https://www.npmjs.com/package/@hapi/joi)
- [`express`](https://www.npmjs.com/package/express)
- [`How express middleware works`](https://expressjs.com/en/guide/using-middleware.html)

## License

MIT

---
_Source: https://npm.io/package/@sloang/joi-middleware · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
