# express-joi-validators

> Joi validation wrapper middleware for validating express request headers and body.

Latest version **1.0.2** (published 2020-03-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-joi-validators
pnpm add express-joi-validators
yarn add express-joi-validators
bun add express-joi-validators
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-03-23 |
| First published | 2020-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | David M.Chen |
| Maintainers | chen7david |
| Keywords | validation, @hapi/joi, joi, validator, express-validation, express, header, body |

## Links

- npm: https://www.npmjs.com/package/express-joi-validators
- Repository: https://github.com/chen7david/express-joi-validators
- Homepage: https://github.com/chen7david/express-joi-validators#readme
- Issues: https://github.com/chen7david/express-joi-validators/issues
- npm.io page: https://npm.io/package/express-joi-validators

## Alternatives

- [@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
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-03-23
- 1.0.1 — 2020-03-14
- 1.0.0 — 2020-03-12
- 0.9.9 — 2020-03-12

## README

# Express Joi Validators (EJV)


### Introduction
This is a Joi validation wrapper middleware for validating express request headers and body. All data validated by express-joi-validators is passed to a context object 'ctx' which is set on the request object of express.

####  1. Getting Started
Here we will show you how to get started with express-joi-validators, through an example. Please follow the steps below to guide you through the process:
- create a new folder on your desktop
- navigate into the folder with your CLI. <code>$ cd your_directory_path</code>
- create a file called <code>index.js</code>
- copy and paste the example code into your <code>index.js</code> file
- run <code>npm init -y</code>
- run <code>npm i express @hapi/joi express-joi-validators</code>
- run node <code>index.js</code>
- open <code>http://localhost:5000</code> in your browser
- you should now get a response similar to the one given in the example below. 

**Note:** If your version of express is lower than 4.16.0 you will have to install the body-parser package and modify part 3 of the example accordingly.

```js 
// PART 1: Setting up express
const express = require('express') 
const app = express()
const server = require('http').createServer(app)
const port = 5000

// PART 2: Require in Joi and express-joi-validators
const Joi = require('@hapi/joi')
const { validateHeader, validateBody } = require('express-joi-validators')

// PART 3: Setting up request JSON and URL parsers. Note: if they are not called, joi will not call next(error)
app.use(express.json()) // This middleware is required for schemas validation to work.
app.use(express.urlencoded({ extended: true }))

// PART 4: Create Joi schema for validating request body
const schema = Joi.object().options({abortEarly: false}).keys({
    username: Joi.string().lowercase().required(),
    password: Joi.string().min(6).required(),
})

// PART 5: Create route with validatorBody mounted
const requestHandler = (req, res) => res.end()
app.use('/', validateBody(schema), requestHandler)


// PART 6: Mount an express errorHandler middleware to catch and handle the Joi validation Error
const errorHandler = (err, req, res, next) => res.status(422).json(err)
app.use(errorHandler) 

// PART 7: Set server to listen for requests
server.listen(port, () => {
    console.log(`server running at http://localhost:${port}`)
})
```
The code above would yield the response below: 

```js 
{
    "_original": {},
    "details": [
        {
            "message": "\"username\" is required",
            "path": [
                "username"
            ],
            "type": "any.required",
            "context": {
                "label": "username",
                "key": "username"
            }
        },
        {
            "message": "\"password\" is required",
            "path": [
                "password"
            ],
            "type": "any.required",
            "context": {
                "label": "password",
                "key": "password"
            }
        }
    ]
}
```

####  2. Getting the Validated Response Body 
If the request passes all validation rules, you access the validated body in your route through <code>req.ctx.body</code>. 

example: 
```js
app.use('/login', validateBody(loginShema), (req, res) => {
    const { body } = req.ctx
    // your code ...
})
```

####  3. Getting the Validated Response Header 
If the request passes all validation rules, you access the validated header in your route through <code>req.ctx.header</code>. 

example: 
```js
app.use('/login', validateBody(loginShema), (req, res) => {
    const { header } = req.ctx
    // your code ...
})
```

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