# express-validation-middleware

> Validation middleware for a node-express server

Latest version **1.0.2** (published 2017-02-11) · ISC license · 0 weekly downloads

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

## Install

```sh
npm install express-validation-middleware
pnpm add express-validation-middleware
yarn add express-validation-middleware
bun add express-validation-middleware
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2017-02-11 |
| First published | 2017-02-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | natashahull |

## Links

- npm: https://www.npmjs.com/package/express-validation-middleware
- Repository: https://github.com/natashahull/express_validation_middleware
- Issues: https://github.com/natashahull/express_validation_middleware/issues
- npm.io page: https://npm.io/package/express-validation-middleware

## Dependencies (1)

- [babel-polyfill](https://npm.io/package/babel-polyfill.md) ^6.13.0

## Recent versions

- 1.0.2 (latest) — 2017-02-11
- 1.0.1 — 2017-02-11
- 1.0.0 — 2017-02-11

## README

#Validation Middleware for node express ![Travis CI](https://travis-ci.org/NatashaHull/express_validation_middleware.svg?branch=master)

## Usage

To use, call the validation middleware
```js
var validationMiddleware = require('express_validation_middleware');
var endTime = Date.now();

validationMiddleware({
  resourceId: {
    target: 'body',
    presence: true,
    in: [1,2,3,4,5],
    between: [0,5],
    custom(resourceId) {
      if (resourceId > 5 || resourceId < 1) return `${resourceId} should be from 1 to 5`;
    }
  },
  startTime: {
    target: 'query',
    custom(startTime) {
      if (startTime >= endTime) return 'startTime must be in the past';
    }
  }
})(req, res, next);
```

If the validation passes, it will call next and continue the request. If the validation fails, it will return a 422 and end the request with a helpful error message.

## Keys

Each top level key is the key on the target object that needs to be validated in some way. In the example case above, the first key can be found on req.body.requestId.

## Target

The target defaults to req.body, but can point to any object on the request with data.

## Validation types
The currently defined validation types are `presence`, `in`, `between`, and `custom`. Any other key in the validation params will be ignored.

### Custom validations
To create a custom validation add a custom key to the validation object that returns a string on failure (and anything other that is not a string on success).

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