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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
var chai = require('chai');
var expect = chai.expect;
var request = require('supertest');
var app;
var errorMsg = 'Parameter is not an integer.';
var errorMsgOutOfRange = 'Parameter is out of range or not int.';
// There are three ways to pass parameters to express:
// - as part of the URL
// - as GET parameter in the querystring
// - as POST parameter in the body
// These test show that req.checkParams are only interested in req.params values, all other
// parameters will be ignored.
var schema = {
testparam: {
in: 'params',
notEmpty: true,
isInt: {
errorMessage: errorMsg
}
},
testquery: {
in: 'query',
notEmpty: true,
isInt: {
options: [{
min: 2,
max: 10
}],
errorMessage: errorMsgOutOfRange
}
},
'skipped': {
// this validator is a fake validator which cannot raise any error, should be always skipped
in: 'notSupportedOne',
notEmpty: true,
isInt: {
options: [{
min: 2,
max: 10
}],
errorMessage: errorMsgOutOfRange
}
},
'numInQuery': {
notEmpty: true,
isInt: {
options: [{
min: 0,
max: 665
}],
errorMessage: errorMsgOutOfRange
}
}
};
function validationSendResponse(req, res) {
var errors = req.validationErrors();
if (errors) {
return res.send(errors);
}
res.send({
testparam: req.params.testparam,
testquery: req.query.testquery,
skipped: req.query.skipped,
numInQuery: req.query.numInQuery
});
}
function validation(req, res) {
req.check(schema);
validationSendResponse(req, res);
}
function validationQuery(req, res) {
req.checkQuery(schema);
validationSendResponse(req, res);
}
function validationParams(req, res) {
req.checkParams(schema);
validationSendResponse(req, res);
}
function validationBody(req, res) {
req.checkBody(schema);
validationSendResponse(req, res);
}
function failParams(body, length) {
expect(body).to.have.length(length);
expect(body[0]).to.have.property('msg', errorMsg);
}
function failQuery(body, length) {
expect(body).to.have.length(length);
expect(body[0]).to.have.property('msg', errorMsgOutOfRange);
}
function failAll(body, length) {
expect(body).to.have.length(length);
expect(body[0]).to.have.property('msg', errorMsg);
expect(body[1]).to.have.property('msg', errorMsgOutOfRange);
}
function pass(params) {
expect(params).to.have.property('testparam', '25');
expect(params).to.have.property('testquery', '6');
expect(params).to.have.property('skipped', '34');
expect(params).to.have.property('numInQuery', '0');
}
function failQueryParams(params, length) {
expect(params).to.have.length(length);
expect(params[0]).to.have.property('msg', 'Invalid param');
expect(params[1]).to.have.property('msg', errorMsgOutOfRange);
}
function getRoute(path, test, length, done) {
request(app)
.get(path)
.end(function(err, res) {
test(res.body, length);
done();
});
}
describe('Check defining validator location inside schema validators', function() {
// 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);
});
it('should validate against schema with query and params locations', function(done) {
getRoute('/25?testquery=6&skipped=34&numInQuery=0', pass, 1, done);
});
it('should fail when param is not integer', function(done) {
getRoute('/ImNot?testquery=6&skipped=34&numInQuery=0', failParams, 1, done);
});
it('should fail when query param is out of range', function(done) {
getRoute('/25?testquery=20&skipped=34&numInQuery=0', failQuery, 1, done);
});
it('should fail when non of params are valid', function(done) {
getRoute('/ImNot?testquery=20&skipped=34&numInQuery=0', failAll, 2, done);
});
});
describe('Check defining validator location inside schema validators by checkQuery()', function() {
// 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')(validationQuery);
});
it('should validate against schema with query and params locations', function(done) {
getRoute('/25?testquery=6&skipped=34&numInQuery=0', pass, 1, done);
});
it('should fail when query param is out of range', function(done) {
getRoute('/25?testquery=6&skipped=34&numInQuery=666', failQuery, 1, done);
});
});
describe('Check defining validator location inside schema validators by checkParams()', function() {
// 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')(validationParams);
});
it('should fail when searching for query param in the path params', function(done) {
getRoute('/25?testquery=6&skipped=34&numInQuery=666', failQueryParams, 2, done);
});
});
describe('Check defining validator location inside schema validators by checkBody()', function() {
// 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')(validationBody);
});
it('should fail when searching for query param in the body', function(done) {
getRoute('/25?testquery=6&skipped=34&numInQuery=666', failQueryParams, 2, done);
});
});
|