# express-validate-requests

> methods to validate and sanatise requests in express.js

Latest version **1.1.2** (published 2013-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-validate-requests
pnpm add express-validate-requests
yarn add express-validate-requests
bun add express-validate-requests
```

## 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.2 |
| Published | 2013-06-05 |
| First published | 2013-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Andrew Griffiths |
| Maintainers | techjacker |

## Links

- npm: https://www.npmjs.com/package/express-validate-requests
- Repository: git@github.com:techjacker/express-validate-requests
- npm.io page: https://npm.io/package/express-validate-requests

## Dependencies (3)

- [validator](https://npm.io/package/validator.md) >=0.5
- [underscore](https://npm.io/package/underscore.md) >=1.4
- [custom-errors](https://npm.io/package/custom-errors.md) >=1.0

## Recent versions

- 1.1.2 (latest) — 2013-06-05
- 1.1.1 — 2013-04-25
- 1.1.0 — 2013-04-24
- 1.0.2 — 2013-04-19
- 1.0.1 — 2013-04-12
- 1.0.0 — 2013-04-12
- 0.1.0 — 2013-04-12

## README

# Express Validate Requests

[![Build Status](https://secure.travis-ci.org/techjacker/express-validate-requests.png)](http://travis-ci.org/techjacker/express-validate-requests)

## QuickStart
#### The following MIDDLEWARE (app.use(fn)) methods are included:
- sanitizeRequest(req, res, next)
- allowJustXHR(req, res, next)
- allowJustJsonRequests(req, res, next)
- allowXOrigin(req, res, next)

If the validation fails then an next(error) will be called. You should set up an error catcher as your last piece of middleware as a catchall for any errors.

#### The following helper fns are included:
- checkGotAllParams(params:{@Object}, requiredParamsKeys:{@Array})

## Full Example

```JavaScript
var express    	= require('express'),
    app        	= express(),
    valExpress 	= require('express-validate-requests'),
    middle 		= valExpress.middleware,
    helpers		= valExpress.helpers,
	allowedHosts = ["api.domainx.com", "www.domainx.com"],
	allowedOrigins = ["http://domainx.com", "http://www.domainx.com"];

app.configure(function() {
	app.use(middle.allowJustXHR);
	app.use(middle.onlyAllowJsonRequests);
	app.use(middle.sanitizeRequest);

	// middle.setAllowCrossOrigin returns fn(req, res, next)
	app.use(middle.setAllowCrossOrigin(allowedHosts, allowedOrigins));
});

app.get('/someroute', function (req,res,next) {
	var requiredParamsKeys = ["paramOneKey", "paramTwoKey"];
	if (!helpers.checkGotAllParams(req.query, requiredParamsKeys)) {
		next(new Error('invalid params'));
	} else {
		next();
	}
});

//... error catcher at very bottom
app.configure('development', function() {
    app.use(express.errorHandler()); // built-in express error handler
});

// listen once configured everything
http.createServer(app).listen(app.get('port'), function() {
	console.log("Express server listening on port: " + app.get('port'));
});
```

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