<commit_before>[![Build Status](https://travis-ci.org/scull7/get-validated.svg)](https://travis-ci.org/scull7/get-validated)
[![Coverage Status](https://coveralls.io/repos/scull7/get-validated/badge.png)](https://coveralls.io/r/scull7/get-validated)
[![Code Climate](https://codeclimate.com/github/scull7/get-validated/badges/gpa.svg)](https://codeclimate.com/github/scull7/get-validated)

get-validated
=============

A simple, flexible validation middleware to make validation less painful

What is it?
-----------
get-validated is not a validation framework. Rather, it is a simple express
middleware that allows you to integrate many validation frameworks into your
express app in a consistent manner.

It comes with the [validator](https://github.com/chriso/validator.js) framework
built in. 

All validation errors are combined and put into a single `ValidationError` object
with a status|code of 412.  And the `messages` property is set to an object keyed
by parameter name of the current errors.

Basic Usage
-----------
Initial set up for a our little example express app.

```javascript
var express = require('express'),
    router  = express.Router(),
    get_validated = require('get-validated'),
    validations
;
```

Once we have our express app all set then we create our 
validations object.

```javascript

validations = get_validated({
  'param1': function (value, container, done) {
      // perform validation here.
      // `value` will be the return from req.param('param1')
      // `container` will be whatever is passed to `get_validated`
      //          `options` parameter as the `options.container` object
      //          It will also have a `helpers` object that will have
      //          the [`validator`](https://github.com/chriso/validator.js)
      //          framework attached.
      if (!container.helpers.isInt(value)) {
        return done(":name must be an integer, ':value' given");
      }
      if (value < 123) {
        return done(":name must be greater than 123, ':value' given");
      }
      
      return done(null, container.helpers.toInt(value));
  },
  'param2': function (value, container, done) {
      // you will have access to the current request via
      // `container.req`
      // You can do async things!
      var req = container.req;
      
      req.db.query('SELECT * FROM something where id = ?', [value],
        function (err, rows) {
          // ... do some validation here...
          return done(':name value of :value could not be found!');
      }
  }
});
```<commit_msg>move async comment above db query.<commit_after>[![Build Status](https://travis-ci.org/scull7/get-validated.svg)](https://travis-ci.org/scull7/get-validated)
[![Coverage Status](https://coveralls.io/repos/scull7/get-validated/badge.png)](https://coveralls.io/r/scull7/get-validated)
[![Code Climate](https://codeclimate.com/github/scull7/get-validated/badges/gpa.svg)](https://codeclimate.com/github/scull7/get-validated)

get-validated
=============

A simple, flexible validation middleware to make validation less painful

What is it?
-----------
get-validated is not a validation framework. Rather, it is a simple express
middleware that allows you to integrate many validation frameworks into your
express app in a consistent manner.

It comes with the [validator](https://github.com/chriso/validator.js) framework
built in. 

All validation errors are combined and put into a single `ValidationError` object
with a status|code of 412.  And the `messages` property is set to an object keyed
by parameter name of the current errors.

Basic Usage
-----------
Initial set up for a our little example express app.

```javascript
var express = require('express'),
    router  = express.Router(),
    get_validated = require('get-validated'),
    validations
;
```

Once we have our express app all set then we create our 
validations object.

```javascript

validations = get_validated({
  'param1': function (value, container, done) {
      // perform validation here.
      // `value` will be the return from req.param('param1')
      // `container` will be whatever is passed to `get_validated`
      //          `options` parameter as the `options.container` object
      //          It will also have a `helpers` object that will have
      //          the [`validator`](https://github.com/chriso/validator.js)
      //          framework attached.
      if (!container.helpers.isInt(value)) {
        return done(":name must be an integer, ':value' given");
      }
      if (value < 123) {
        return done(":name must be greater than 123, ':value' given");
      }
      
      return done(null, container.helpers.toInt(value));
  },
  'param2': function (value, container, done) {
      // you will have access to the current request via
      // `container.req`
      var req = container.req;
      
      // You can do async things!
      req.db.query('SELECT * FROM something where id = ?', [value],
        function (err, rows) {
          // ... do some validation here...
          return done(':name value of :value could not be found!');
      }
  }
});
```
{'fraction_non_alphanumeric': 0.13189168127800507, 'fraction_numerical': 0.0070134424313267095, 'mean_word_length': 2.940138142747506, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2572732', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1455, 'n_words': 489}