forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmtpapi_validator.js
More file actions
2708 lines (2437 loc) · 79.6 KB
/
Copy pathsmtpapi_validator.js
File metadata and controls
2708 lines (2437 loc) · 79.6 KB
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"bRhrlU":[function(require,module,exports){
var validator = require('jsonschema').Validator;
var v = new validator();
var headerSchema = {
"id" : "Header",
"type": "object",
"properties": {
"to": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"minItems": 1
},
"category": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"minItems": 1
},
"section": {
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
}
},
"sub": {
"type": "object",
"patternProperties": {
".+": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
}
}
},
"unique_args": {
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
}
},
"filters": {
"type": "object",
"patternProperties": {
".+": {
"type": "object",
"properties": {
"settings": {
"required": true,
"type": "object",
"patternProperties": {
".+": {
"type": ["string", "number"]
}
}
}
}
}
}
}
}
};
var smtpapivalidator = function (header) {
return v.validate(header, headerSchema).errors;
};
module.exports = exports = smtpapivalidator;
},{"jsonschema":5}],"smtpapivalidator":[function(require,module,exports){
module.exports=require('bRhrlU');
},{}],3:[function(require,module,exports){
'use strict';
var helpers = require('./helpers');
/** @type ValidatorResult */
var ValidatorResult = helpers.ValidatorResult;
/** @type SchemaError */
var SchemaError = helpers.SchemaError;
var attribute = {};
attribute.ignoreProperties = {
// informative properties
'id': true,
'default': true,
'description': true,
'title': true,
// arguments to other properties
'exclusiveMinimum': true,
'exclusiveMaximum': true,
'additionalItems': true,
// special-handled properties
'$schema': true,
'$ref': true,
'extends': true
};
/**
* @name validators
*/
var validators = attribute.validators = {};
/**
* Validates whether the instance if of a certain type
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null}
*/
validators.type = function validateType (instance, schema, options, ctx) {
// Ignore undefined instances
if (instance === undefined) {
return null;
}
var types = (schema.type instanceof Array) ? schema.type : [schema.type];
if (!types.some(this.testType.bind(this, instance, schema, options, ctx))) {
return "is not of a type(s) " + types.map(function (v) {
return v.id && ('<' + v.id + '>') || v.toString();
});
}
return null;
};
function testSchema(instance, options, ctx, schema){
return this.validateSchema(instance, schema, options, ctx).valid;
}
/**
* Validates whether the instance matches some of the given schemas
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null}
*/
validators.anyOf = function validateAnyOf (instance, schema, options, ctx) {
// Ignore undefined instances
if (instance === undefined) {
return null;
}
if (!(schema.anyOf instanceof Array)){
throw new SchemaError("anyOf must be an array");
}
if (!schema.anyOf.some(testSchema.bind(this, instance, options, ctx))) {
return "is not any of " + schema.anyOf.map(function (v) {
return v.id && ('<' + v.id + '>') || v.toString();
});
}
return null;
};
/**
* Validates whether the instance matches every given schema
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null}
*/
validators.allOf = function validateAllOf (instance, schema, options, ctx) {
// Ignore undefined instances
if (instance === undefined) {
return null;
}
if (!(schema.allOf instanceof Array)){
throw new SchemaError("allOf must be an array");
}
if (!schema.allOf.every(testSchema.bind(this, instance, options, ctx))) {
return "is not all from " + schema.allOf.map(function (v) {
return v.id && ('<' + v.id + '>') || v.toString();
});
}
return null;
};
/**
* Validates whether the instance matches exactly one of the given schemas
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null}
*/
validators.oneOf = function validateOneOf (instance, schema, options, ctx) {
// Ignore undefined instances
if (instance === undefined) {
return null;
}
if (!(schema.oneOf instanceof Array)){
throw new SchemaError("oneOf must be an array");
}
var count = schema.oneOf.filter(testSchema.bind(this, instance, options, ctx)).length;
if (count!==1) {
return "is not exactly one from " + schema.oneOf.map(function (v) {
return v.id && ('<' + v.id + '>') || v.toString();
});
}
return null;
};
/**
* Validates properties
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null|ValidatorResult}
*/
validators.properties = function validateProperties (instance, schema, options, ctx) {
if(instance === undefined) return;
var result = new ValidatorResult(instance, schema, options, ctx);
var properties = schema.properties || {};
for (var property in properties) {
var prop = (instance || undefined) && instance[property];
var res = this.validateSchema(prop, properties[property], options, ctx.makeChild(properties[property], property));
if(res.instance !== result.instance[property]) result.instance[property] = res.instance;
result.importErrors(res);
}
return result;
};
/**
* Test a specific property within in instance against the additionalProperties schema attribute
* This ignores properties with definitions in the properties schema attribute, but no other attributes.
* If too many more types of property-existance tests pop up they may need their own class of tests (like `type` has)
* @private
* @return {boolean}
*/
function testAdditionalProperty (instance, schema, options, ctx, property, result) {
if (schema.properties && schema.properties[property] !== undefined) {
return;
}
if (schema.additionalProperties === false) {
result.addError("Property " + property + " does not exist in the schema");
} else {
var additionalProperties = schema.additionalProperties || {};
var res = this.validateSchema(instance[property], additionalProperties, options, ctx.makeChild(additionalProperties, property));
if(res.instance !== result.instance[property]) result.instance[property] = res.instance;
result.importErrors(res);
}
}
/**
* Validates patternProperties
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null|ValidatorResult}
*/
validators.patternProperties = function validatePatternProperties (instance, schema, options, ctx) {
if(instance === undefined) return;
if(!this.types.object(instance)) return;
var result = new ValidatorResult(instance, schema, options, ctx);
var patternProperties = schema.patternProperties || {};
for (var property in instance) {
var test = true;
for (var pattern in patternProperties) {
var expr = new RegExp(pattern);
if (!expr.test(property)) {
continue;
}
test = false;
var res = this.validateSchema(instance[property], patternProperties[pattern], options, ctx.makeChild(patternProperties[pattern], property));
if(res.instance !== result.instance[property]) result.instance[property] = res.instance;
result.importErrors(res);
}
if (test) {
testAdditionalProperty.call(this, instance, schema, options, ctx, property, result);
}
}
return result;
};
/**
* Validates additionalProperties
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null|ValidatorResult}
*/
validators.additionalProperties = function validateAdditionalProperties (instance, schema, options, ctx) {
if(instance === undefined) return;
if(!this.types.object(instance)) return;
// if patternProperties is defined then we'll test when that one is called instead
if (schema.patternProperties) {
return null;
}
var result = new ValidatorResult(instance, schema, options, ctx);
for (var property in instance) {
testAdditionalProperty.call(this, instance, schema, options, ctx, property, result);
}
return result;
};
/**
* Validates items when instance is an array
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null|ValidatorResult}
*/
validators.items = function validateItems (instance, schema, options, ctx) {
if (!(instance instanceof Array)) {
return null;
}
var self = this;
var result = new ValidatorResult(instance, schema, options, ctx);
if (instance === undefined || !schema.items) {
return result;
}
instance.every(function (value, i) {
var items = (schema.items instanceof Array) ? (schema.items[i] || schema.additionalItems) : schema.items;
if (items === undefined) {
return true;
}
if (items === false) {
result.addError("additionalItems not permitted");
return false;
}
var res = self.validateSchema(value, items, options, ctx.makeChild(items, i));
result.instance[i] = res.instance;
result.importErrors(res);
return true;
});
return result;
};
/**
* Validates minimum and exclusiveMinimum when the type of the instance value is a number.
* @param instance
* @param schema
* @return {String|null}
*/
validators.minimum = function validateMinimum (instance, schema) {
if (typeof instance !== 'number') {
return null;
}
var valid = true;
if (schema.exclusiveMinimum && schema.exclusiveMinimum === true) {
valid = instance > schema.minimum;
} else {
valid = instance >= schema.minimum;
}
if (!valid) {
return "is not " + schema.minimum;
}
return null;
};
/**
* Validates maximum and exclusiveMaximum when the type of the instance value is a number.
* @param instance
* @param schema
* @return {String|null}
*/
validators.maximum = function validateMaximum (instance, schema) {
if (typeof instance !== 'number') {
return null;
}
var valid;
if (schema.exclusiveMaximum && schema.exclusiveMaximum === true) {
valid = instance < schema.maximum;
} else {
valid = instance <= schema.maximum;
}
if (!valid) {
return "is not " + schema.maximum;
}
return null;
};
/**
* Validates divisibleBy when the type of the instance value is a number.
* Of course, this is susceptible to floating point error since it compares the floating points
* and not the JSON byte sequences to arbitrary precision.
* @param instance
* @param schema
* @return {String|null}
*/
validators.divisibleBy = function validateDivisibleBy (instance, schema) {
if (typeof instance !== 'number') {
return null;
}
if (schema.divisibleBy == 0) {
throw new SchemaError("divisibleBy cannot be zero");
}
if (instance / schema.divisibleBy % 1) {
return "is not " + schema.divisibleBy;
}
return null;
};
/**
* Validates whether the instance value is present.
* @param instance
* @param schema
* @return {String|null}
*/
validators.required = function validateRequired (instance, schema) {
if (instance === undefined && schema.required === true) {
return "is required";
}
return null;
};
/**
* Validates whether the instance value matches the regular expression, when the instance value is a string.
* @param instance
* @param schema
* @return {String|null}
*/
validators.pattern = function validatePattern (instance, schema) {
if (typeof instance !== 'string') {
return null;
}
if (!instance.match(schema.pattern)) {
return "does not match pattern" + schema.pattern;
}
return null;
};
/**
* Validates whether the instance value is of a certain defined format, when the instance value is a string.
* The following format are supported:
* - date-time
* - date
* - time
* - ip-address
* - ipv6
* - uri
* - color
* - host-name
* - alpha
* - alpha-numeric
* - utc-millisec
* @param instance
* @param schema
* @param [options]
* @param [ctx]
* @return {String|null}
*/
validators.format = function validateFormat (instance, schema, options, ctx) {
if (instance === undefined) {
return null;
}
if (!helpers.isFormat(instance, schema.format)) {
return "does not conform to the '" + schema.format + "' format";
}
return null;
};
/**
* Validates whether the instance value is at least of a certain length, when the instance value is a string.
* @param instance
* @param schema
* @return {String|null}
*/
validators.minLength = function validateMinLength (instance, schema) {
if (!(typeof instance === 'string')) {
return null;
}
if (!(instance.length >= schema.minLength)) {
return "does not meet minimum length of " + schema.minLength;
}
return null;
};
/**
* Validates whether the instance value is at most of a certain length, when the instance value is a string.
* @param instance
* @param schema
* @return {String|null}
*/
validators.maxLength = function validateMaxLength (instance, schema) {
if (!(typeof instance === 'string')) {
return null;
}
if (!(instance.length <= schema.maxLength)) {
return "does not meet maximum length of " + schema.maxLength;
}
return null;
};
/**
* Validates whether instance contains at least a minimum number of items, when the instance is an Array.
* @param instance
* @param schema
* @return {String|null}
*/
validators.minItems = function validateMinItems (instance, schema) {
if (!(instance instanceof Array)) {
return null;
}
if (!(instance.length >= schema.minItems)) {
return "does not meet minimum length of " + schema.minItems;
}
return null;
};
/**
* Validates whether instance contains no more than a maximum number of items, when the instance is an Array.
* @param instance
* @param schema
* @return {String|null}
*/
validators.maxItems = function validateMaxItems (instance, schema) {
if (!(instance instanceof Array)) {
return null;
}
if (!(instance.length <= schema.maxItems)) {
return "does not meet maximum length of " + schema.maxItems;
}
return null;
};
/**
* Validates that every item in an instance array is unique, when instance is an array
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null|ValidatorResult}
*/
validators.uniqueItems = function validateUniqueItems (instance, schema, options, ctx) {
var result = new ValidatorResult(instance, schema, options, ctx);
if (!(instance instanceof Array)) {
return result;
}
function testArrays (v, i, a) {
for (var j = i + 1; j < a.length; j++) if (helpers.deepCompareStrict(v, a[j])) {
return false;
}
return true;
}
if (!instance.every(testArrays)) {
result.addError("contains duplicate item");
}
return result;
};
/**
* Deep compares arrays for duplicates
* @param v
* @param i
* @param a
* @private
* @return {boolean}
*/
function testArrays (v, i, a) {
var j, len = a.length;
for (j = i + 1, len; j < len; j++) {
if (helpers.deepCompareStrict(v, a[j])) {
return false;
}
}
return true;
}
/**
* Validates whether there are no duplicates, when the instance is an Array.
* @param instance
* @return {String|null}
*/
validators.uniqueItems = function validateUniqueItems (instance) {
if (!(instance instanceof Array)) {
return null;
}
if (!instance.every(testArrays)) {
return "contains duplicate item";
}
return null;
};
/**
* Validate for the presence of dependency properties, if the instance is an object.
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null|ValidatorResult}
*/
validators.dependencies = function validateDependencies (instance, schema, options, ctx) {
var result = new ValidatorResult(instance, schema, options, ctx);
if (!instance || typeof instance != 'object') {
return null;
}
for (var property in schema.dependencies) {
if (instance[property] === undefined) {
continue;
}
var dep = schema.dependencies[property];
var childContext = ctx.makeChild(dep, property);
if (typeof dep == 'string') {
dep = [dep];
}
if (dep instanceof Array) {
dep.forEach(function (prop) {
if (instance[prop] === undefined) {
result.addError("property " + prop + " not found, required by " + childContext.propertyPath);
}
});
} else {
var res = this.validateSchema(instance, dep, options, childContext);
result.instance[property] = res.instance;
if (res && res.errors.length) {
result.addError("does not meet dependency required by " + childContext.propertyPath);
result.importErrors(res);
}
}
}
return result;
};
/**
* Validates whether the instance value is one of the enumerated values.
*
* @param instance
* @param schema
* @return {String|null}
*/
validators.enum = function validateEnum (instance, schema) {
if (!(schema.enum instanceof Array)) {
throw new SchemaError("enum expects an array", schema);
}
if (instance === undefined) {
instance = schema.default;
}
if (!schema.enum.some(helpers.deepCompareStrict.bind(null, instance))) {
return "is not one of enum values: " + schema.enum;
}
return null;
};
/**
* Validates whether the instance if of a prohibited type.
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null|ValidatorResult}
*/
validators.not = validators.disallow = function validateNot (instance, schema, options, ctx) {
var self = this;
var result = new ValidatorResult(instance, schema, options, ctx);
var types = (schema.disallow instanceof Array) ? schema.disallow : [schema.disallow];
types.forEach(function (type) {
if (self.testType(instance, schema, options, ctx, type)) {
var schemaId = type && type.id && ('<' + type.id + '>') || type.toString();
result.addError("is of prohibited type " + schemaId);
}
});
return result;
};
module.exports = attribute;
},{"./helpers":4}],4:[function(require,module,exports){
'use strict';
var uri = require('url');
var ValidationError = exports.ValidationError = function ValidationError (message, instance, schema, propertyPath) {
if (propertyPath) {
this.property = propertyPath;
}
if (message) {
this.message = message;
}
if (schema) {
if (schema.id) {
this.schema = schema.id;
} else {
this.schema = schema;
}
}
if (instance) {
this.instance = instance;
}
this.stack = this.toString();
};
ValidationError.prototype.toString = function toString() {
return this.property + ' ' + this.message;
};
var ValidatorResult = exports.ValidatorResult = function ValidatorResult(instance, schema, options, ctx) {
this.instance = instance;
this.schema = schema;
this.propertyPath = ctx.propertyPath;
this.errors = [];
this.throwError = options && options.throwError;
};
ValidatorResult.prototype.addError = function addError(message) {
var err = new ValidationError(message, this.instance, this.schema, this.propertyPath);
if (this.throwError) {
throw err;
}
this.errors.push(err);
return err;
};
ValidatorResult.prototype.importErrors = function importErrors(res) {
if (typeof res == 'string') {
this.addError(res);
} else if (res && res.errors) {
var errs = this.errors;
res.errors.forEach(function (v) {
errs.push(v)
});
}
};
ValidatorResult.prototype.toString = function toString(res) {
return this.errors.map(function(v,i){ return i+': '+v.toString()+'\n'; }).join('');
};
Object.defineProperty(ValidatorResult.prototype, "valid", { get: function() {
return !this.errors.length;
} });
/**
* Describes a problem with a Schema which prevents validation of an instance
* @name SchemaError
* @constructor
*/
var SchemaError = exports.SchemaError = function SchemaError (msg, schema) {
this.message = msg;
this.schema = schema;
Error.call(this, msg);
Error.captureStackTrace(this, SchemaError);
};
SchemaError.prototype = Object.create(Error.prototype,
{ constructor: {value: SchemaError, enumerable: false}
, name: {value: 'SchemaError', enumerable: false}
});
var SchemaContext = exports.SchemaContext = function SchemaContext (schema, options, propertyPath, base, schemas) {
this.schema = schema;
this.options = options;
this.propertyPath = propertyPath;
this.base = base;
this.schemas = schemas;
};
SchemaContext.prototype.resolve = function resolve (target) {
return uri.resolve(this.base, target);
};
SchemaContext.prototype.makeChild = function makeChild(schema, propertyName){
var propertyPath = (propertyName===undefined) ? this.propertyPath : this.propertyPath+makeSuffix(propertyName);
var base = uri.resolve(this.base, schema.id||'');
var ctx = new SchemaContext(schema, this.options, propertyPath, base, Object.create(this.schemas));
if(schema.id && !ctx.schemas[base]){
ctx.schemas[base] = schema;
}
return ctx;
}
var FORMAT_REGEXPS = exports.FORMAT_REGEXPS = {
'date-time': /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/,
'date': /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-[0-9]{2}$/,
'time': /^\d{2}:\d{2}:\d{2}$/,
'email': /^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/,
'ip-address': /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
'ipv6': /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/,
'uri': /^[a-zA-Z][a-zA-Z0-9+-.]*:[^\s]*$/,
'color': /(#?([0-9A-Fa-f]{3,6})\b)|(aqua)|(black)|(blue)|(fuchsia)|(gray)|(green)|(lime)|(maroon)|(navy)|(olive)|(orange)|(purple)|(red)|(silver)|(teal)|(white)|(yellow)|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\))/,
'host-name': /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/,
'alpha': /^[a-zA-Z]+$/,
'alphanumeric': /^[a-zA-Z0-9]+$/,
'utc-millisec': function (input) {
return (typeof input === 'string') && parseFloat(input) === parseInt(input, 10) && !isNaN(input);
},
'regex': function (input) {
var result = true;
try {
new RegExp(input);
} catch (e) {
result = false;
}
return result;
},
'style': /\s*(.+?):\s*([^;]+);?/g,
'phone': /^\+(?:[0-9] ?){6,14}[0-9]$/
};
FORMAT_REGEXPS.regexp = FORMAT_REGEXPS.regex;
FORMAT_REGEXPS.pattern = FORMAT_REGEXPS.regex;
FORMAT_REGEXPS.ipv4 = FORMAT_REGEXPS['ip-address'];
exports.isFormat = function isFormat (input, format) {
if (FORMAT_REGEXPS[format] !== undefined) {
if (FORMAT_REGEXPS[format] instanceof RegExp) {
return FORMAT_REGEXPS[format].test(input);
}
if (typeof FORMAT_REGEXPS[format] === 'function') {
return FORMAT_REGEXPS[format](input);
}
}
return false;
};
var makeSuffix = exports.makeSuffix = function makeSuffix (key) {
key = key.toString();
// This function could be capable of outputting valid a ECMAScript string, but the
// resulting code for testing which form to use would be tens of thousands of characters long
// That means this will use the name form for some illegal forms
if (!key.match(/[.\s\[\]]/) && !key.match(/^[\d]/)) {
return '.' + key;
}
if (key.match(/^\d+$/)) {
return '[' + key + ']';
}
return '[' + JSON.stringify(key) + ']';
};
exports.deepCompareStrict = function deepCompareStrict (a, b) {
if (typeof a !== typeof b) {
return false;
}
if (a instanceof Array) {
if (!(b instanceof Array)) {
return false;
}
if (a.length !== b.length) {
return false;
}
return a.every(function (v, i) {
return deepCompareStrict(a[i], b[i]);
});
}
if (typeof a === 'object') {
if (!a || !b) {
return a === b;
}
var aKeys = Object.keys(a);
var bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
return false;
}
return aKeys.every(function (v) {
return deepCompareStrict(a[v], b[v]);
});
}
return a === b;
};
module.exports.deepMerge = function deepMerge (target, src) {
var array = Array.isArray(src);
var dst = array && [] || {};
if (array) {
target = target || [];
dst = dst.concat(target);
src.forEach(function (e, i) {
if (typeof e === 'object') {
dst[i] = deepMerge(target[i], e)
} else {
if (target.indexOf(e) === -1) {
dst.push(e)
}
}
});
} else {
if (target && typeof target === 'object') {
Object.keys(target).forEach(function (key) {
dst[key] = target[key];
});
}
Object.keys(src).forEach(function (key) {
if (typeof src[key] !== 'object' || !src[key]) {
dst[key] = src[key];
}
else {
if (!target[key]) {
dst[key] = src[key];
} else {
dst[key] = deepMerge(target[key], src[key])
}
}
});
}
return dst;
};
/**
* Validates instance against the provided schema
* Implements URI+JSON Pointer encoding, e.g. "%7e"="~0"=>"~", "~1"="%2f"=>"/"
* @param o
* @param s The path to walk o along
* @return any
*/
exports.objectGetPath = function objectGetPath(o, s) {
var parts = s.split('/').slice(1);
var k;
while (typeof (k=parts.shift()) == 'string') {
var n = decodeURIComponent(k.replace(/~0/,'~').replace(/~1/g,'/'));
if (!(n in o)) return;
o = o[n];
}
return o;
};
/**
* Accept an Array of property names and return a JSON Pointer URI fragment
* @param Array a
* @return {String}
*/
exports.encodePath = function encodePointer(a){
// ~ must be encoded explicitly because hacks
// the slash is encoded by encodeURIComponent
return a.map(function(v){ return '/'+encodeURIComponent(v).replace(/~/g,'%7E'); }).join('');
}
},{"url":11}],5:[function(require,module,exports){
'use strict';
var Validator = module.exports.Validator = require('./validator');
module.exports.ValidatorResult = require('./helpers').ValidatorResult;
module.exports.ValidationError = require('./helpers').ValidationError;
module.exports.SchemaError = require('./helpers').SchemaError;
module.exports.validate = function (instance, schema, options) {
var v = new Validator();
return v.validate(instance, schema, options);
};
},{"./helpers":4,"./validator":6}],6:[function(require,module,exports){
'use strict';
var urilib = require('url');
var attribute = require('./attribute');
var helpers = require('./helpers');
var ValidatorResult = helpers.ValidatorResult;
var SchemaError = helpers.SchemaError;
var SchemaContext = helpers.SchemaContext;
/**
* Creates a new Validator object
* @name Validator
* @constructor
*/
var Validator = function Validator () {
this.schemas = {};
this.unresolvedRefs = [];
// Use Object.create to make this extensible without Validator instances stepping on each other's toes.
this.types = Object.create(types);
this.attributes = Object.create(attribute.validators);
};
// Hint at the presence of a property
Validator.prototype.schemas = null;
Validator.prototype.types = null;
Validator.prototype.attributes = null;
Validator.prototype.unresolvedRefs = null;
/**
* Adds a schema with a certain urn to the Validator instance.
* @param schema
* @param urn
* @return {Object}
*/
Validator.prototype.addSchema = function addSchema (schema, uri) {
if (!schema) {
return null;
}
var ourUri = uri || schema.id;
this.addSubSchema(ourUri, schema);
if (ourUri) {