aboutsummaryrefslogtreecommitdiff
path: root/node_modules/express-validator/test/helpers/app.js
blob: d86a4bb0f05605952ab00f47b68fcc6b9f5e3d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Sample app
var express = require('express');
var expressValidator = require('../../index');
var bodyParser = require('body-parser');

var port = process.env.PORT || 8888;
var app = express();

// If no native implementation of Promise exists (less than Node v4),
// use Bluebird promises so we can test for both depending on the Node version
if (typeof Promise === 'undefined') {
  Promise = require('bluebird');
}

module.exports = function(validation) {

  app.set('port', port);
  app.use(bodyParser.json());
  app.use(expressValidator({
    customValidators: {
      isArray: function(value) {
        return Array.isArray(value);
      },
      isAsyncTest: function(testparam) {
        return new Promise(function(resolve, reject) {
          setTimeout(function() {
            if (testparam === '42') { return resolve(); }
            reject();
          }, 200);
        });
      }
    },
    customSanitizers: {
      toTestSanitize: function() {
        return "!!!!";
      }
    }
  }));

  app.get(/\/test(\d+)/, validation);
  app.get('/:testparam?', validation);
  app.post('/:testparam?', validation);

  return app;
};