This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
/
validators.js
669 lines (570 loc) · 20.1 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Apigee Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
'use strict';
var _ = require('lodash');
var helpers = require('./helpers');
// http://tools.ietf.org/html/rfc3339#section-5.6
var dateRegExp = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
// http://tools.ietf.org/html/rfc3339#section-5.6
var dateTimeRegExp = /^([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]+)?(z|([+-][0-9]{2}):([0-9]{2}))$/;
var isValidDate = module.exports.isValidDate = function (date) {
var day;
var matches;
var month;
if (_.isDate(date)) {
return true;
}
if (!_.isString(date)) {
date = date.toString();
}
matches = dateRegExp.exec(date);
if (matches === null) {
return false;
}
day = matches[3];
month = matches[2];
if (month < '01' || month > '12' || day < '01' || day > '31') {
return false;
}
return true;
};
var isValidDateTime = module.exports.isValidDateTime = function (dateTime) {
var hour;
var date;
var time;
var matches;
var minute;
var parts;
var second;
var timezoneHours;
var timezoneMinutes;
if (_.isDate(dateTime)) {
return true;
}
if (!_.isString(dateTime)) {
dateTime = dateTime.toString();
}
parts = dateTime.toLowerCase().split('t');
date = parts[0];
time = parts.length > 1 ? parts[1] : undefined;
if (!isValidDate(date)) {
return false;
}
matches = dateTimeRegExp.exec(time);
if (matches === null) {
return false;
}
hour = matches[1];
minute = matches[2];
second = matches[3];
if (matches[5] === 'z') {
timezoneHours = 0;
timezoneMinutes = 0;
} else {
timezoneHours = Number(matches[6]);
timezoneMinutes = Number(matches[7]);
}
var validTimezoneMinutes = timezoneMinutes === 0 || timezoneMinutes === 15 || timezoneMinutes === 30 || timezoneMinutes === 45;
if (hour > '23' || minute > '59' || second > '59' || timezoneHours > 14 || timezoneHours < -12 || !validTimezoneMinutes) {
return false;
}
return true;
};
var throwErrorWithCode = function (code, msg) {
var err = new Error(msg);
err.code = code;
err.failedValidation = true;
throw err;
};
module.exports.validateAgainstSchema = function (schemaOrName, data, validator) {
var sanitizeError = function (obj) {
// Make anyOf/oneOf errors more human readable (Issue 200)
var defType = ['additionalProperties', 'items'].indexOf(obj.path[obj.path.length - 1]) > -1 ?
'schema' :
obj.path[obj.path.length - 2];
if (['ANY_OF_MISSING', 'ONE_OF_MISSING'].indexOf(obj.code) > -1) {
switch (defType) {
case 'parameters':
defType = 'parameter';
break;
case 'responses':
defType = 'response';
break;
case 'schema':
defType += ' ' + obj.path[obj.path.length - 1];
// no default
}
obj.message = 'Not a valid ' + defType + ' definition';
}
// Remove the params portion of the error
delete obj.params;
delete obj.schemaId;
if (obj.inner) {
_.each(obj.inner, function (nObj) {
sanitizeError(nObj);
});
}
};
var schema = _.isPlainObject(schemaOrName) ? _.cloneDeep(schemaOrName) : schemaOrName;
// We don't check this due to internal usage but if validator is not provided, schemaOrName must be a schema
if (_.isUndefined(validator)) {
validator = helpers.createJsonValidator([schema]);
}
var valid = validator.validate(data, schema);
if (!valid) {
try {
throwErrorWithCode('SCHEMA_VALIDATION_FAILED', 'Failed schema validation');
} catch (err) {
err.results = {
errors: _.map(validator.getLastErrors(), function (err) {
sanitizeError(err);
return err;
}),
warnings: []
};
throw err;
}
}
};
/**
* Validates a schema of type array is properly formed (when necessar).
*
* *param {object} schema - The schema object to validate
*
* @throws Error if the schema says it's an array but it is not formed properly
*
* @see {@link https://github.com/swagger-api/swagger-spec/issues/174}
*/
var validateArrayType = module.exports.validateArrayType = function (schema) {
// We have to do this manually for now
if (schema.type === 'array' && _.isUndefined(schema.items)) {
throwErrorWithCode('OBJECT_MISSING_REQUIRED_PROPERTY', 'Missing required property: items');
}
};
/**
* Validates the request or response content type (when necessary).
*
* @param {string[]} gPOrC - The valid consumes at the API scope
* @param {string[]} oPOrC - The valid consumes at the operation scope
* @param {object} reqOrRes - The request or response
*
* @throws Error if the content type is invalid
*/
module.exports.validateContentType = function (gPOrC, oPOrC, reqOrRes) {
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1
var isResponse = typeof reqOrRes.end === 'function';
var contentType = isResponse ? reqOrRes.getHeader('content-type') : reqOrRes.headers['content-type'];
var pOrC = _.map(_.union(gPOrC, oPOrC), function (contentType) {
return contentType.split(';')[0];
});
if (!contentType) {
if (isResponse) {
contentType = 'text/plain';
} else {
contentType = 'application/octet-stream';
}
}
contentType = contentType.split(';')[0];
if (pOrC.length > 0 && (isResponse ?
true :
['POST', 'PUT'].indexOf(reqOrRes.method) !== -1) && pOrC.indexOf(contentType) === -1) {
throw new Error('Invalid content type (' + contentType + '). These are valid: ' + pOrC.join(', '));
}
};
/**
* Validates the value against the allowable values (when necessary).
*
* @param {*} val - The parameter value
* @param {string[]} allowed - The allowable values
*
* @throws Error if the value is not allowable
*/
var validateEnum = module.exports.validateEnum = function (val, allowed) {
if (!_.isUndefined(allowed) && !_.isUndefined(val) && allowed.indexOf(val) === -1) {
throwErrorWithCode('ENUM_MISMATCH', 'Not an allowable value (' + allowed.join(', ') + '): ' + val);
}
};
/**
* Validates the value is less than the maximum (when necessary).
*
* @param {*} val - The parameter value
* @param {string} maximum - The maximum value
* @param {boolean} [exclusive=false] - Whether or not the value includes the maximum in its comparison
*
* @throws Error if the value is greater than the maximum
*/
var validateMaximum = module.exports.validateMaximum = function (val, maximum, type, exclusive) {
var code = exclusive === true ? 'MAXIMUM_EXCLUSIVE' : 'MAXIMUM';
var testMax;
var testVal;
if (_.isUndefined(exclusive)) {
exclusive = false;
}
if (type === 'integer') {
testVal = parseInt(val, 10);
} else if (type === 'number') {
testVal = parseFloat(val);
}
if (!_.isUndefined(maximum)) {
testMax = parseFloat(maximum);
if (exclusive && testVal >= testMax) {
throwErrorWithCode(code, 'Greater than or equal to the configured maximum (' + maximum + '): ' + val);
} else if (testVal > testMax) {
throwErrorWithCode(code, 'Greater than the configured maximum (' + maximum + '): ' + val);
}
}
};
/**
* Validates the array count is less than the maximum (when necessary).
*
* @param {*[]} val - The parameter value
* @param {number} maxItems - The maximum number of items
*
* @throws Error if the value contains more items than allowable
*/
var validateMaxItems = module.exports.validateMaxItems = function (val, maxItems) {
if (!_.isUndefined(maxItems) && val.length > maxItems) {
throwErrorWithCode('ARRAY_LENGTH_LONG', 'Array is too long (' + val.length + '), maximum ' + maxItems);
}
};
/**
* Validates the value length is less than the maximum (when necessary).
*
* @param {*[]} val - The parameter value
* @param {number} maxLength - The maximum length
*
* @throws Error if the value's length is greater than the maximum
*/
var validateMaxLength = module.exports.validateMaxLength = function (val, maxLength) {
if (!_.isUndefined(maxLength) && val.length > maxLength) {
throwErrorWithCode('MAX_LENGTH', 'String is too long (' + val.length + ' chars), maximum ' + maxLength);
}
};
/**
* Validates the value's property count is greater than the maximum (when necessary).
*
* @param {*[]} val - The parameter value
* @param {number} minProperties - The maximum number of properties
*
* @throws Error if the value's property count is less than the maximum
*/
var validateMaxProperties = module.exports.validateMaxProperties = function (val, maxProperties) {
var propCount = _.isPlainObject(val) ? Object.keys(val).length : 0;
if (!_.isUndefined(maxProperties) && propCount > maxProperties) {
throwErrorWithCode('MAX_PROPERTIES',
'Number of properties is too many (' + propCount + ' properties), maximum ' + maxProperties);
}
};
/**
* Validates the value array count is greater than the minimum (when necessary).
*
* @param {*} val - The parameter value
* @param {string} minimum - The minimum value
* @param {boolean} [exclusive=false] - Whether or not the value includes the minimum in its comparison
*
* @throws Error if the value is less than the minimum
*/
var validateMinimum = module.exports.validateMinimum = function (val, minimum, type, exclusive) {
var code = exclusive === true ? 'MINIMUM_EXCLUSIVE' : 'MINIMUM';
var testMin;
var testVal;
if (_.isUndefined(exclusive)) {
exclusive = false;
}
if (type === 'integer') {
testVal = parseInt(val, 10);
} else if (type === 'number') {
testVal = parseFloat(val);
}
if (!_.isUndefined(minimum)) {
testMin = parseFloat(minimum);
if (exclusive && testVal <= testMin) {
throwErrorWithCode(code, 'Less than or equal to the configured minimum (' + minimum + '): ' + val);
} else if (testVal < testMin) {
throwErrorWithCode(code, 'Less than the configured minimum (' + minimum + '): ' + val);
}
}
};
/**
* Validates the value value contains fewer items than allowed (when necessary).
*
* @param {*[]} val - The parameter value
* @param {number} minItems - The minimum number of items
*
* @throws Error if the value contains fewer items than allowable
*/
var validateMinItems = module.exports.validateMinItems = function (val, minItems) {
if (!_.isUndefined(minItems) && val.length < minItems) {
throwErrorWithCode('ARRAY_LENGTH_SHORT', 'Array is too short (' + val.length + '), minimum ' + minItems);
}
};
/**
* Validates the value length is less than the minimum (when necessary).
*
* @param {*[]} val - The parameter value
* @param {number} minLength - The minimum length
*
* @throws Error if the value's length is less than the minimum
*/
var validateMinLength = module.exports.validateMinLength = function (val, minLength) {
if (!_.isUndefined(minLength) && val.length < minLength) {
throwErrorWithCode('MIN_LENGTH', 'String is too short (' + val.length + ' chars), minimum ' + minLength);
}
};
/**
* Validates the value's property count is less than or equal to the minimum (when necessary).
*
* @param {*[]} val - The parameter value
* @param {number} minProperties - The minimum number of properties
*
* @throws Error if the value's property count is less than the minimum
*/
var validateMinProperties = module.exports.validateMinProperties = function (val, minProperties) {
var propCount = _.isPlainObject(val) ? Object.keys(val).length : 0;
if (!_.isUndefined(minProperties) && propCount < minProperties) {
throwErrorWithCode('MIN_PROPERTIES',
'Number of properties is too few (' + propCount + ' properties), minimum ' + minProperties);
}
};
/**
* Validates the value is a multiple of the provided number (when necessary).
*
* @param {*[]} val - The parameter value
* @param {number} multipleOf - The number that should divide evenly into the value
*
* @throws Error if the value contains fewer items than allowable
*/
var validateMultipleOf = module.exports.validateMultipleOf = function (val, multipleOf) {
if (!_.isUndefined(multipleOf) && val % multipleOf !== 0) {
throwErrorWithCode('MULTIPLE_OF', 'Not a multiple of ' + multipleOf);
}
};
/**
* Validates the value matches a pattern (when necessary).
*
* @param {string} name - The parameter name
* @param {*} val - The parameter value
* @param {string} pattern - The pattern
*
* @throws Error if the value does not match the pattern
*/
var validatePattern = module.exports.validatePattern = function (val, pattern) {
if (!_.isUndefined(pattern) && _.isNull(val.match(new RegExp(pattern)))) {
throwErrorWithCode('PATTERN', 'Does not match required pattern: ' + pattern);
}
};
/**
* Validates the value requiredness (when necessary).
*
* @param {*} val - The parameter value
* @param {boolean} required - Whether or not the parameter is required
*
* @throws Error if the value is required but is not present
*/
module.exports.validateRequiredness = function (val, required) {
if (!_.isUndefined(required) && required === true && _.isUndefined(val)) {
throwErrorWithCode('REQUIRED', 'Is required');
}
};
/**
* Validates the value type and format (when necessary).
*
* @param {string} version - The Swagger version
* @param {*} val - The parameter value
* @param {string} type - The parameter type
* @param {string} format - The parameter format
* @param {boolean} [skipError=false] - Whether or not to skip throwing an error (Useful for validating arrays)
*
* @throws Error if the value is not the proper type or format
*/
var validateTypeAndFormat = module.exports.validateTypeAndFormat =
function validateTypeAndFormat (version, val, type, format, allowEmptyValue, skipError) {
var result = true;
var oVal = val;
// If there is an empty value and we allow empty values, the value is always valid
if (allowEmptyValue === true && val === '') {
return;
}
if (_.isArray(val)) {
_.each(val, function (aVal, index) {
if (!validateTypeAndFormat(version, aVal, type, format, allowEmptyValue, true)) {
throwErrorWithCode('INVALID_TYPE', 'Value at index ' + index + ' is not a valid ' + type + ': ' + aVal);
}
});
} else {
switch (type) {
case 'boolean':
// Coerce the value only for Swagger 1.2
if (version === '1.2' && _.isString(val)) {
if (val === 'false') {
val = false;
} else if (val === 'true') {
val = true;
}
}
result = _.isBoolean(val);
break;
case 'integer':
// Coerce the value only for Swagger 1.2
if (version === '1.2' && _.isString(val)) {
val = Number(val);
}
result = _.isFinite(val) && (Math.round(val) === val);
break;
case 'number':
// Coerce the value only for Swagger 1.2
if (version === '1.2' && _.isString(val)) {
val = Number(val);
}
result = _.isFinite(val);
break;
case 'string':
if (!_.isUndefined(format)) {
switch (format) {
case 'date':
result = isValidDate(val);
break;
case 'date-time':
result = isValidDateTime(val);
break;
}
}
break;
case 'void':
result = _.isUndefined(val);
break;
}
}
if (skipError) {
return result;
} else if (!result) {
throwErrorWithCode('INVALID_TYPE',
type !== 'void' ?
'Not a valid ' + (_.isUndefined(format) ? '' : format + ' ') + type + ': ' + oVal :
'Void does not allow a value');
}
};
/**
* Validates the value values are unique (when necessary).
*
* @param {string[]} val - The parameter value
* @param {boolean} isUnique - Whether or not the parameter values are unique
*
* @throws Error if the value has duplicates
*/
var validateUniqueItems = module.exports.validateUniqueItems = function (val, isUnique) {
if (!_.isUndefined(isUnique) && _.uniq(val).length !== val.length) {
throwErrorWithCode('ARRAY_UNIQUE', 'Does not allow duplicate values: ' + val.join(', '));
}
};
/**
* Validates the value against the schema.
*
* @param {string} version - The Swagger version
* @param {object} schema - The schema to use to validate things
* @param {string[]} path - The path to the schema
* @param {*} [val] - The value to validate or undefined to use the default value provided by the schema
*
* @throws Error if any validation failes
*/
var validateSchemaConstraints = module.exports.validateSchemaConstraints = function (version, schema, path, val) {
var resolveSchema = function (schema) {
var resolved = schema;
if (resolved.schema) {
path = path.concat(['schema']);
resolved = resolveSchema(resolved.schema);
}
return resolved;
};
var type = schema.type;
var allowEmptyValue;
if (!type) {
if (!schema.schema) {
if (path[path.length - 2] === 'responses') {
type = 'void';
} else {
type = 'object';
}
} else {
schema = resolveSchema(schema);
type = schema.type || 'object';
}
}
allowEmptyValue = schema ? schema.allowEmptyValue === true : false;
try {
// Always perform this check even if there is no value
if (type === 'array') {
validateArrayType(schema);
}
// Default to default value if necessary
if (_.isUndefined(val)) {
val = version === '1.2' ? schema.defaultValue : schema.default;
path = path.concat([version === '1.2' ? 'defaultValue' : 'default']);
}
// If there is no explicit default value, return as all validations will fail
if (_.isUndefined(val)) {
return;
}
if (type === 'array') {
_.each(val, function (val, index) {
try {
validateSchemaConstraints(version, schema.items || {}, path.concat(index.toString()), val);
} catch (err) {
err.message = 'Value at index ' + index + ' ' + (err.code === 'INVALID_TYPE' ? 'is ' : '') +
err.message.charAt(0).toLowerCase() + err.message.substring(1);
throw err;
}
});
} else {
validateTypeAndFormat(version, val, type, schema.format, allowEmptyValue);
}
// Validate enum
validateEnum(val, schema.enum);
// Validate maximum
validateMaximum(val, schema.maximum, type, schema.exclusiveMaximum);
// Validate maxItems (Swagger 2.0+)
validateMaxItems(val, schema.maxItems);
// Validate maxLength (Swagger 2.0+)
validateMaxLength(val, schema.maxLength);
// Validate maxProperties (Swagger 2.0+)
validateMaxProperties(val, schema.maxProperties);
// Validate minimum
validateMinimum(val, schema.minimum, type, schema.exclusiveMinimum);
// Validate minItems
validateMinItems(val, schema.minItems);
// Validate minLength (Swagger 2.0+)
validateMinLength(val, schema.minLength);
// Validate minProperties (Swagger 2.0+)
validateMinProperties(val, schema.minProperties);
// Validate multipleOf (Swagger 2.0+)
validateMultipleOf(val, schema.multipleOf);
// Validate pattern (Swagger 2.0+)
validatePattern(val, schema.pattern);
// Validate uniqueItems
validateUniqueItems(val, schema.uniqueItems);
} catch (err) {
err.path = path;
throw err;
}
};