aboutsummaryrefslogtreecommitdiff
path: root/node_modules/express-validator/test/withMessageTest.js
blob: ad38684e3d01a50a2a869b8201e2cbb8825127b6 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var chai = require('chai');
var expect = chai.expect;
var request = require('supertest');

var app;
var errorMessage = 'Default error message';
var mustBeTwoDigitsMessage = 'testparam must have two digits';
var mustBeIntegerMessage = 'testparam must be an integer';

function validation(req, res) {
  req.checkParams('testparam', errorMessage)
    .notEmpty() // with default message
    .isInt().withMessage(mustBeIntegerMessage)
    .isInt() // with default message
    .isLength({ min: 2, max: 2 }).withMessage(mustBeTwoDigitsMessage);
  var errors = req.validationErrors();
  if (errors) {
    return res.send(errors);
  }
  res.send({ testparam: req.params.testparam });
}

function fail(expectedMessage) {
  if (Array.isArray(expectedMessage)) {
    return function(body, length) {
      expect(body).to.have.length(length);
      expect(expectedMessage).to.have.length(length);
      for (var i = 0; i < length; ++i) {
        expect(body[i]).to.have.property('msg', expectedMessage[i]);
      }
    };
  }
  return function(body, length) {
    expect(body).to.have.length(length);
    expect(body[0]).to.have.property('msg', expectedMessage);
  };
}

function pass(body) {
  expect(body).to.have.property('testparam', '42');
}

function getRoute(path, test, length, done) {
  request(app)
    .get(path)
    .end(function(err, res) {
      test(res.body, length);
      done();
    });
}

// This before() is required in each set of tests in
// order to use a new validation function in each file
before(function() {
  delete require.cache[require.resolve('./helpers/app')];
  app = require('./helpers/app')(validation);
});

describe('#withMessage()', function() {
    it('should return one error per validation failure, with custom message where defined', function(done) {
      getRoute('/test', fail([mustBeIntegerMessage, errorMessage, mustBeTwoDigitsMessage]), 3, done);
    });

    it('should return four errors when param is missing, with default message for the first and third errors, and custom messages for the rest, as defined', function(done) {
      getRoute('/', fail([errorMessage, mustBeIntegerMessage, errorMessage, mustBeTwoDigitsMessage]), 4, done);
    });

    it('should return a success when param validates', function(done) {
      getRoute('/42', pass, null, done);
    });

    it('should provide a custom message when an invalid value is provided, and the validation is followed by withMessage', function(done) {
      getRoute('/199', fail(mustBeTwoDigitsMessage), 1, done);
    });

    it('should update the error message only if the preceeding validation was the one to fail', function() {
      var validator = require('../lib/express_validator')();
      var req = {
        body: {
          testParam: 'abc'
        }
      };

      validator(req, {}, function() {});
      req.check('testParam', 'Default Error Message')
        .isInt() // should produce 'Default Error Message'
        .isLength({ min: 2 }).withMessage('Custom Error Message');

      expect(req.validationErrors()).to.deep.equal([
        { param: 'testParam', msg: 'Default Error Message', value: 'abc' }
      ]);
    });
});