This repository has been archived by the owner on Feb 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
validators.js
346 lines (290 loc) · 8.37 KB
/
validators.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Licensed to Cloudkick, Inc ('Cloudkick') under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* Cloudkick licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var fs = require('fs');
var path = require('path');
var T = require('magic-templates');
var async = require('async');
var sprintf = require('sprintf').sprintf;
var misc = require('util/misc');
var health = require('health');
var constants = require('manifest/constants');
var Errorf = misc.Errorf;
/**
* Validators functions for the manifest file values.
*
* @param {Varies} value Value which is being validated.
* @param {Object} options Options objects which contains additional information about the manifest.
* @param {Function} callback Callback which is called with the error as the first argument if the validation fails,
* without argument otherwise.
*/
function isValidType(value, options, callback) {
if (!misc.inArray(value, constants.APPLICATION_TYPES)) {
callback(new Errorf('Invalid type: %s', value));
return;
}
callback();
}
function isValidString(value, options, callback) {
if (typeof(value) === 'string') {
callback();
return;
}
else if (typeof(value) === 'object') {
if (value instanceof String) {
callback();
return;
}
}
callback(new Error('value is not a string'));
}
function isValidNumber(value, options, callback) {
if (typeof(value) === 'number') {
callback();
return;
}
else if (typeof(value) === 'object') {
if (value instanceof Number) {
callback();
return;
}
}
callback(new Error('value is not a number'));
}
function isValidObject(value, options, callback) {
if ((typeof(value) === 'object') && (value !== null)) {
callback();
return;
}
callback(new Error('value is not an object'));
}
function isValidArray(value, options, callback) {
if ((value instanceof Array)) {
callback();
return;
}
callback(new Error('value is not an array'));
}
function isValidPort(value, options, callback) {
isValidNumber(value, options, function(error) {
if (error) {
callback(error);
return;
}
if (!((value > 0) && (value <= 65535))) {
callback(new Error('Port number must be between 1 and 65535'));
return;
}
callback();
});
}
function isValidCheck(value, options, callback) {
var checkName, checkModule, checkClass, index;
if (typeof(value) !== 'object') {
callback(new Error('Health check item, must be an object'));
return;
}
if (!value.hasOwnProperty('check')) {
callback(new Error('Missing "check" property'));
return;
}
checkName = value.check;
// Check for a valid check name
index = misc.arrayFind(checkName,
health.availableChecks,
null,
function(item, needle) {
return item[0] === needle;
});
if (index === false) {
callback(new Error(sprintf('Invalid check type: %s', checkName)));
return;
}
checkModule = require(sprintf('health/checks/%s', checkName));
checkClass = health.availableChecks[index][1];
// Create a check object which throws an exception if a required argument is missing
try {
var module = new checkModule[checkClass](value['arguments']);
}
catch (error) {
callback(error);
return;
}
callback();
}
function isValidTemplate(value, options, callback) {
if (!value) {
callback(new Error('Template file is an empty string'));
return;
}
var templatePath = path.join(options.manifest_path, value);
path.exists(templatePath, function(exists) {
if (!exists) {
callback(new Error('Template file does not exist'));
return;
}
var readStream = fs.createReadStream(templatePath);
var dataBuffer = [];
readStream.on('data', function(chunk) {
dataBuffer.push(chunk);
});
readStream.on('end', function() {
var data = dataBuffer.join('');
var template = new T.Template();
try {
template.parse(data);
}
catch (error) {
callback(error);
return;
}
callback();
});
});
}
function isValidDataFile(value, options, callback) {
isValidString(value, options, function(error) {
if (error) {
callback(error);
return;
}
if (value.substr(0, 1) === '/' ||
value.substr(0, 2) === '~/' ||
value.substr(0, 2) === './' ||
value.substr(0, 2) === '..') {
callback(new Error('All the paths must be relative to the application root directory'));
return;
}
callback();
});
}
function isValidTemplateVariable(value, options, callback) {
var item;
if (typeof(value) !== 'object' || value instanceof Array) {
callback(new Error('Template variable item must be an object'));
return;
}
function validateItem(item) {
async.waterfall([
function(callback) {
isValidString(item, options, function(error) {
if (error) {
callback(null, false);
return;
}
callback(null, true);
});
},
function(isValid, callback) {
if (isValid) {
callback();
return;
}
isValidNumber(item, options, function(error) {
callback(error);
});
}
],
function(error) {
if (error) {
callback(new Error('Template variable values can only be strings or integers'));
return;
}
callback();
});
}
for (var key in value) {
if (value.hasOwnProperty(key)) {
item = value[key];
validateItem(item);
}
}
}
function isValidBundleVersion(value, options, callback) {
var err = null;
if (value.indexOf('@') !== -1) {
err = new Error('Version number cannot contain @ character');
}
callback(err);
}
/**
* Validator name to function mappings
* @const
*/
var VALIDATORS = {
'valid_type': isValidType,
'valid_string': isValidString,
'valid_number': isValidNumber,
'valid_object': isValidObject,
'valid_array': isValidArray,
'valid_port': isValidPort,
'valid_check': isValidCheck,
'valid_template': isValidTemplate,
'valid_data_file': isValidDataFile,
'valid_template_variable': isValidTemplateVariable,
'valid_version': isValidBundleVersion
};
/**
* Variable type to validator name mapping
* @const
*/
var TYPE_VALIDATORS = {
'string': 'valid_string',
'number': 'valid_number',
'object': 'valid_object',
'array': 'valid_array'
};
/**
* Applies validator to an Array of values
*
* @param {Array} array Array of items to validate.
* @param {String} validator Validator to run against values.
* @param {?Object} options Extra options to the Validator.
* @param {Function} callback Invoked on completion, first parameter
* is present if errors happened.
*/
function validateArray(array, validator, options, callback) {
async.forEach(array, function(item, callback) {
VALIDATORS[validator].call(null, item, options, function(error) {
callback(error);
});
},
function(error) {
if (error) {
callback(error);
return;
}
callback();
});
}
/**
* Applies validator to a single value.
*
* @param {Object} value Item to validate.
* @param {String} validator Validator to run against values.
* @param {?Object} options Extra options to the Validator.
* @param {Function} callback Invoked on completion, first parameter
* is present if errors happened.
*/
function validateValue(value, validator, options, callback) {
VALIDATORS[validator].call(null, value, options, function(error) {
callback(error);
});
}
exports.VALIDATORS = VALIDATORS;
exports.TYPE_VALIDATORS = TYPE_VALIDATORS;
exports.validateArray = validateArray;
exports.validateValue = validateValue;