# middleware-params-validator

> A simple middleware to validate the request object (body, params, querystring, headers, customData, ...) againts a json structure

Latest version **1.1.0** (published 2018-11-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install middleware-params-validator
pnpm add middleware-params-validator
yarn add middleware-params-validator
bun add middleware-params-validator
```

## 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.1.0 |
| Published | 2018-11-11 |
| First published | 2018-07-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | João Moura |
| Maintainers | jwebcoder |
| Keywords | middleware, express, parameters, body, validation, validator, json |

## Links

- npm: https://www.npmjs.com/package/middleware-params-validator
- Repository: https://github.com/JWebCoder/middleware-params-validator
- Homepage: https://github.com/JWebCoder/middleware-params-validator#readme
- Issues: https://github.com/JWebCoder/middleware-params-validator/issues
- npm.io page: https://npm.io/package/middleware-params-validator

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2018-11-11
- 1.0.0 — 2018-07-15

## README

# middleware-params-validator

A simple middleware to validate the request object (body, params, querystring, headers, customData, ...) againts a json structure

## Requirements

node >= 6.9

## usage

Simply add the validator middleware to your routes with the desired json structure

```js
{
  /* body: just an object inside the request */
  body: {
    /* parameter of the body */
    email: [    /* array of validations to execute    */
      'email',  /* just checking if its a valid email */
    ],          /*------------------------------------*/
  },
  /* params: another object inside the request */
  params: 'id', /* you can also pass a string instead of an object, this means that it will only check if the parameter exists */
}
```

### extra data on validations

```js
{
  /* custom: just an object inside the request */
  params: [
    /* parameter of the custom object */
    'id',
    /* another parameter of the custom object */
    'date',
    /* and yet another parameter of the custom object */
    {
      name: [
        {
          /* this is an example, on how to pass extra data to a validator */
          length: 'min_5',
        },
      ],
    },
  ]
}
```

### custom data on request

If you have a controller/middleware/... that adds data to the request object, you can also check it

```js
{
  /* custom: just an object inside the request */
  custom: [
    /* parameter of the custom object */
    {
      email: [
        'email',
      ],
    },
    /* another parameter of the custom object */
    'date',
    /* and yet another parameter of the custom object */
    {
      name: [
        {
          /* this is an example, on how to pass extra data to a validator */
          length: 'min_5',
        },
      ],
    },
  ]
}
```

## available validators

- email
- length (supports extra data: min_N, max_N, exact_N)
- number
- words (supports extra data that is the minimum number of words: N )

## example

```js
// controller
import friendController from 'controllers/friend'

// middleware
import validator from 'middleware-params-validator'

const router = Router()

router.post('/:id/addFriend',
  validator({
    body: {
      email: [
        'email',
      ],
    },
    params: 'id',
  }),
  someController.addFriend
)

router.delete('/:id/removeFriend/:email',
  validator({
    params: [
      'id',
      {
        'email': [
          'email',
        ],
      },
    ],
  }),
  friendController.removeFriend
)

router.delete('/:id',
  validator({
    params: 'id',
  }),
  friendController.deleteHoliday
)

router.post('/',
  validator({
    body: {
      email: [
        'email',
      ],
    },
  }),
  friendController.addFriend
)

```

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