-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlx-valid.js
1325 lines (1143 loc) · 47.8 KB
/
lx-valid.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
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
/*!
* lx-valid - v1.2.4 - 2016-11-16
* https://github.com/litixsoft/lx-valid
*
* Copyright (c) 2016 Litixsoft GmbH
* Licensed MIT
*/
(function (exports) {
'use strict';
/**
* Gets the type of the value in lower case.
*
* @param {*} value The value to test.
* @returns {string}
*/
function getType(value) {
// inspired by http://techblog.badoo.com/blog/2013/11/01/type-checking-in-javascript/
// handle null in old IE
if (value === null) {
return 'null';
}
// handle DOM elements
if (value && (value.nodeType === 1 || value.nodeType === 9)) {
return 'element';
}
var s = Object.prototype.toString.call(value);
var type = s.match(/\[object (.*?)\]/)[1].toLowerCase();
// handle NaN and Infinity
if (type === 'number') {
if (isNaN(value)) {
return 'nan';
}
if (!isFinite(value)) {
return 'infinity';
}
}
return type;
}
//
// ### function validate (object, schema, options)
// #### {Object} object the object to validate.
// #### {Object} schema (optional) the JSON Schema to validate against.
// #### {Object} options (optional) options controlling the validation
// process. See {@link #validate.defaults) for details.
// Validate <code>object</code> against a JSON Schema.
// If <code>object</code> is self-describing (i.e. has a
// <code>$schema</code> property), it will also be validated
// against the referenced schema. [TODO]: This behaviour bay be
// suppressed by setting the {@link #validate.options.???}
// option to <code>???</code>.[/TODO]
//
// If <code>schema</code> is not specified, and <code>object</code>
// is not self-describing, validation always passes.
//
// <strong>Note:</strong> in order to pass options but no schema,
// <code>schema</code> <em>must</em> be specified in the call to
// <code>validate()</code>; otherwise, <code>options</code> will
// be interpreted as the schema. <code>schema</code> may be passed
// as <code>null</code>, <code>undefinded</code>, or the empty object
// (<code>{}</code>) in this case.
//
function validate(object, schema, options) {
options = mixin({}, validate.defaults, options);
if (options.trim === true) {
// ensure that trim works in old browsers
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
}
var errors = [];
// handle array root element
if (schema.items) {
validateProperty(object, object, '', schema, options, errors);
} else {
validateObject(object, schema, options, errors);
}
return {
valid: !(errors.length),
errors: errors
};
}
/**
* Default validation options. Defaults can be overridden by
* passing an 'options' hash to {@link #validate}. They can
* also be set globally be changing the values in
* <code>validate.defaults</code> directly.
*/
validate.defaults = {
/**
* <p>
* Enforce 'format' constraints.
* </p><p>
* <em>Default: <code>true</code></em>
* </p>
*/
validateFormats: true,
/**
* <p>
* When {@link #validateFormats} is <code>true</code>,
* treat unrecognized formats as validation errors.
* </p><p>
* <em>Default: <code>false</code></em>
* </p>
*
* @see validation.formats for default supported formats.
*/
validateFormatsStrict: false,
/**
* <p>
* When {@link #validateFormats} is <code>true</code>,
* also validate formats defined in {@link validate.formatExtensions}.
* </p><p>
* <em>Default: <code>true</code></em>
* </p>
*/
validateFormatExtensions: true,
/**
* <p>
* When {@link #addMissingDefaults} is <code>true</code>,
* if property is missing and it has a default value it will be added to the object.
* </p><p>
* <em>Default: <code>false</code></em>
* </p>
*/
addMissingDefaults: false,
/**
* <p>
* When {@link #unknownProperties} is set to <code>'delete'</code>,
* if property is not declared in schema it is deleted from object.
* When set to <code>'error'</code>, the property will not be deleted and an error is created.
* When set to <code>'ignore'</code>, the property will not be deleted.
* </p><p>
* <em>Default: <code>ignore</code></em>
* </p>
*/
unknownProperties: 'ignore',
/**
* <p>
* When {@link #trim} is <code>true</code>,
* all string values are trimmed.
* </p><p>
* <em>Default: <code>false</code></em>
* </p>
*/
trim: false,
/**
* <p>
* When {@link #strictRequired} is <code>true</code>,
* all empty string values ('') which are required will be invalid.
* </p><p>
* <em>Default: <code>false</code></em>
* </p>
*/
strictRequired: false,
/**
* <p>
* When {@link #ignoreNullValues} is <code>true</code>,
* all values which are null (value === null) will not be validated.
* </p><p>
* <em>Default: <code>false</code></em>
* </p>
*/
ignoreNullValues: false,
/**
* <p>
* When {@link #transform} is a <code>Function</code>,
* all values are passed to that function after validation and can be processed.
* </p><p>
* <em>Default: <code>null</code></em>
* </p>
*/
transform: null
};
/**
* Default messages to include with validation errors.
*/
validate.messages = {
required: 'is required',
minLength: 'is too short (minimum is %{expected} characters)',
maxLength: 'is too long (maximum is %{expected} characters)',
pattern: 'invalid input',
minimum: 'must be greater than or equal to %{expected}',
maximum: 'must be less than or equal to %{expected}',
exclusiveMinimum: 'must be greater than %{expected}',
exclusiveMaximum: 'must be less than %{expected}',
divisibleBy: 'must be divisible by %{expected}',
minItems: 'must contain more than %{expected} items',
maxItems: 'must contain less than %{expected} items',
uniqueItems: 'must hold a unique set of values',
format: 'is not a valid %{expected}',
conform: 'must conform to given constraint',
type: 'must be of %{expected} type',
additionalProperties: 'must not exist',
unknown: 'is not defined in schema'
};
validate.messages['enum'] = 'must be present in given enumerator';
/**
*
*/
validate.formats = {
'email': /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,
'ip-address': /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/i,
'ipv6': /^([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}$/,
'date-time': /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:.\d{1,3})?Z$/,
'date': /^\d{4}-\d{2}-\d{2}$/,
'time': /^\d{2}:\d{2}:\d{2}$/,
'color': /^#[a-z0-9]{6}|#[a-z0-9]{3}|(?:rgb\(\s*(?:[+-]?\d+%?)\s*,\s*(?:[+-]?\d+%?)\s*,\s*(?:[+-]?\d+%?)\s*\))aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow$/i,
//'style': (not supported)
//'phone': (not supported)
//'uri': (not supported)
'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])/,
'utc-millisec': {
test: function (value) {
return typeof (value) === 'number' && value >= 0;
}
},
'regex': {
test: function (value) {
try { new RegExp(value); }
catch (e) { return false; }
return true;
}
},
'mongo-id': /^[0-9a-fA-F]{8}[0-9a-fA-F]{6}[0-9a-fA-F]{4}[0-9a-fA-F]{6}$/,
'uuid': /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
'luuid': /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
'number-float': /^[\-\+]?\b(\d+[.]\d+$)$/,
'float': /^[\-\+]?\b(\d+[.]\d+$)$/,
'integer': /^[\-\+]?[0-9]+$/,
'timestamp': /^[0-9-]+,[\s]*[0-9-]+/,
'empty': /^$/
};
/**
*
*/
validate.formatExtensions = {
'url': /^(https?|ftp|git):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
};
validate.types = {
string: function (value) {
return getType(value) === 'string';
},
number: function (value) {
return getType(value) === 'number';
},
boolean: function (value) {
return getType(value) === 'boolean';
},
date: function (value) {
return getType(value) === 'date';
},
regexp: function (value) {
return getType(value) === 'regexp';
},
array: function (value) {
return getType(value) === 'array';
},
object: function (value) {
return getType(value) === 'object';
},
integer: function (value) {
return getType(value) === 'number' && Math.floor(value) === value;
},
float: function (value) {
return getType(value) === 'number' && new RegExp(validate.formats.float).exec(value);
},
null: function (value) {
return getType(value) === 'null';
},
undefined: function (value) {
return getType(value) === 'undefined';
},
any: function (value) {
return value !== undefined;
},
mongoId: function (value) {
return value && typeof value === 'object' && value._bsontype === 'ObjectID' && validate.formats['mongo-id'].test(value);
},
dbRef: function (value) {
return value && typeof value === 'object' && value._bsontype === 'DBRef' && !!value.namespace && validate.types.string(value.namespace) && !!value.oid;
},
minKey: function (value) {
return value && typeof value === 'object' && value._bsontype === 'MinKey';
},
maxKey: function (value) {
return value && typeof value === 'object' && value._bsontype === 'MaxKey';
},
code: function (value) {
return value && typeof value === 'object' && value._bsontype === 'Code' && !!value.code && validate.types.string(value.code) && !!value.scope && typeof value.scope === 'object';
},
infinity: function (value) {
return getType(value) === 'infinity';
},
nan: function (value) {
return getType(value) === 'nan';
}
};
function setValueInArray(data, oldValue, newValue) {
if (getType(data) === 'array') {
// find item in array
var index = data.indexOf(oldValue);
if (index > -1) {
// set value directly
data[data.indexOf(oldValue)] = newValue;
} else {
var i = 0;
var length = data.length;
// set value recursively
for (i; i < length; i++) {
setValueInArray(data[i], oldValue, newValue);
}
}
}
}
function getTypeCheckFunction(type) {
var result = validate.types[type];
if (!result) {
result = validate.types[type.toLowerCase()];
}
return result;
}
function mixin(obj) {
var sources = Array.prototype.slice.call(arguments, 1);
while (sources.length) {
var source = sources.shift();
if (!source) { continue; }
if (typeof (source) !== 'object') {
throw new TypeError('mixin non-object');
}
for (var p in source) {
if (source.hasOwnProperty(p)) {
obj[p] = source[p];
}
}
}
return obj;
}
function validateObject(object, schema, options, errors) {
var props, p, allProps = Object.keys(object),
visitedProps = [], requiredArray = [];
// required array (schema draft 4)
if (getType(schema.required) === 'array') {
requiredArray = schema.required;
} else if (getType(schema._required) === 'array') {
requiredArray = schema._required;
}
// see 5.2
if (schema.properties) {
props = schema.properties;
for (p in props) {
if (props.hasOwnProperty(p) && getType(props[p]) === 'object') {
var propertySchema = props[p];
if (requiredArray.indexOf(p) > -1) {
// save required array of current property
propertySchema._required = propertySchema.required || [];
// set required to true for validation
propertySchema.required = true;
}
visitedProps.push(p);
validateProperty(object, object[p], p, props[p], options, errors);
} else {
console.log('Schema invalid. Property ' + p + ' is not of type object, actual type: ' + getType(props[p]));
}
}
}
// see 5.3
if (schema.patternProperties) {
props = schema.patternProperties;
for (p in props) {
if (props.hasOwnProperty(p) && getType(props[p]) === 'object') {
var re = new RegExp(p);
// Find all object properties that are matching `re`
for (var k in object) {
if (object.hasOwnProperty(k)) {
if (re.exec(k) !== null) {
validateProperty(object, object[k], k, props[p], options, errors);
visitedProps.push(k);
}
}
}
} else {
console.log('Schema invalid. Property ' + p + ' is not of type object, actual type: ' + getType(props[p]));
}
}
}
// deleteUnknownProperties
if (options.deleteUnknownProperties) {
for (p in object) {
if (object.hasOwnProperty(p)) {
if (visitedProps.indexOf(p) === -1) {
delete object[p];
}
}
}
}
// unknownProperties
if (options.unknownProperties) {
for (p in object) {
if (object.hasOwnProperty(p)) {
if (visitedProps.indexOf(p) === -1) {
if (options.unknownProperties === 'delete') {
delete object[p];
} else if (options.unknownProperties === 'error') {
error('unknown', p, object[p], schema, errors);
}
}
}
}
}
// see 5.4
if (undefined !== schema.additionalProperties) {
var i, l;
var unvisitedProps = allProps.filter(function (k) {
return -1 === visitedProps.indexOf(k);
});
// Prevent additional properties; each unvisited property is therefore an error
if (schema.additionalProperties === false && unvisitedProps.length > 0) {
for (i = 0, l = unvisitedProps.length; i < l; i++) {
error('additionalProperties', unvisitedProps[i], object[unvisitedProps[i]], false, errors);
}
}
// additionalProperties is a schema and validate unvisited properties against that schema
else if (typeof schema.additionalProperties === 'object' && unvisitedProps.length > 0) {
for (i = 0, l = unvisitedProps.length; i < l; i++) {
validateProperty(object, object[unvisitedProps[i]], unvisitedProps[i], schema.unvisitedProperties, options, errors);
}
}
}
}
function validateProperty(object, value, property, schema, options, errors) {
var format, valid, spec, i, l;
var isRequired = schema.required === true;
function constrain(name, value, assert) {
if (schema[name] !== undefined && !assert(value, schema[name], object)) {
error(name, property, value, schema, errors);
}
}
if (options.ignoreNullValues && value === null && !isRequired) {
return;
}
// check for values of type string and option strictRequired
if (typeof value === 'string' && options.strictRequired === true && isRequired) {
// perform trim before checking for required
var valueToTest = options.trim === true ? value.trim() : value;
if (valueToTest === '') {
return error('required', property, undefined, schema, errors);
}
}
if (value === undefined) {
if (schema['default'] !== undefined && options.addMissingDefaults) {
if (typeof schema['default'] === 'function') {
object[property] = value = schema['default']();
} else {
object[property] = value = schema['default'];
}
constrain('conform', value, function (a, e, o) { return e(a, o); });
} else if (isRequired && schema.type !== 'any') {
return error('required', property, undefined, schema, errors);
} else {
constrain('conform', value, function (a, e, o) { return e(a, o); });
return;
}
}
if (options.cast) {
if ('integer' === schema.type || 'number' === schema.type || 'float' === schema.type) {
if (value !== null && typeof value !== 'boolean' && !isNaN(+value)) {
if (getType(object[property]) === 'array') {
setValueInArray(object[property], value, +value);
} else {
object[property] = +value;
}
value = +value;
}
}
if ('boolean' === schema.type) {
if ('true' === value || '1' === value || 1 === value) {
if (getType(object[property]) === 'array') {
setValueInArray(object[property], value, true);
} else {
object[property] = true;
}
value = true;
}
if ('false' === value || '0' === value || 0 === value) {
if (getType(object[property]) === 'array') {
setValueInArray(object[property], value, false);
} else {
object[property] = false;
}
value = false;
}
}
}
if (schema.format && options.validateFormats && typeof value === 'string') {
var formats = isArray(schema.format) ? schema.format : [schema.format];
valid = false;
// Go through available formats
// And find first matching
for (i = 0, l = formats.length; i < l; i++) {
format = formats[i].toLowerCase().trim();
if (options.validateFormatExtensions) {
spec = validate.formatExtensions[format];
}
if (!spec) {
spec = validate.formats[format];
}
if (!spec) {
if (options.validateFormatsStrict) {
valid = false;
break;
}
} else {
if (!spec.test(value)) {
valid = false;
} else {
if (options.convert && typeof options.convert === 'function') {
// try to convert property by schema format
if (isArray(object[property]) || isArray(object)) {
var arr = isArray(object) ? object : object[property];
// convert values in array
var index = arr.indexOf(value);
arr[index] = options.convert(format, value);
} else {
object[property] = options.convert(format, value);
}
}
valid = true;
break;
}
}
}
if (!valid) {
return error('format', property, value, schema, errors);
}
}
/* jshint ignore:start */
if (schema['enum']) {
if (value === null && isArray(schema.type) && schema.type.indexOf('null') > -1) {
// is allowed
} else if (schema['type'] === 'array' && isArray(value)) {
for (i = 0; i < value.length; i++) {
if (schema['enum'].indexOf(value[i]) === -1) {
error('enum', property, value, schema, errors);
break;
}
}
} else if (schema['enum'].indexOf(value) === -1) {
error('enum', property, value, schema, errors);
}
}
/* jshint ignore:end */
// Dependencies (see 5.8)
if (typeof schema.dependencies === 'string' &&
object[schema.dependencies] === undefined) {
error('dependencies', property, null, schema, errors);
}
if (isArray(schema.dependencies)) {
for (i = 0, l = schema.dependencies.length; i < l; i++) {
if (object[schema.dependencies[i]] === undefined) {
error('dependencies', property, null, schema, errors);
}
}
}
if (typeof schema.dependencies === 'object') {
validateObject(object, schema.dependencies, options, errors);
}
checkType(value, schema.type, function (err, type) {
if (err) {
return error('type', property, getType(value), schema, errors);
}
constrain('conform', value, function (a, e, o) { return e(a, o); });
switch (type || (isArray(value) ? 'array' : typeof value)) {
case 'string':
if (options.trim === true) {
if (isArray(object[property])) {
// find value in array
var index = object[property].indexOf(value);
// only trim when value in object is still a string
if (typeof object[property][index] === 'string') {
object[property][index] = value.trim();
}
} else if (typeof object[property] === 'string') {
object[property] = value.trim();
}
}
constrain('minLength', value.length, function (a, e) { return a >= e; });
constrain('maxLength', value.length, function (a, e) { return a <= e; });
constrain('pattern', value, function (a, e) {
e = typeof e === 'string' ? e = new RegExp(e) : e;
return e.test(a);
});
break;
case 'integer':
case 'number':
constrain('minimum', value, function (a, e) { return a >= e; });
constrain('maximum', value, function (a, e) { return a <= e; });
constrain('exclusiveMinimum', value, function (a, e) { return a > e; });
constrain('exclusiveMaximum', value, function (a, e) { return a < e; });
constrain('divisibleBy', value, function (a, e) {
var multiplier = Math.max((a - Math.floor(a)).toString().length - 2, (e - Math.floor(e)).toString().length - 2);
multiplier = multiplier > 0 ? Math.pow(10, multiplier) : 1;
return (a * multiplier) % (e * multiplier) === 0;
});
break;
case 'array':
constrain('items', value, function (a, e) {
var nestedErrors;
for (var i = 0, l = a.length; i < l; i++) {
nestedErrors = [];
validateProperty(object, a[i], property, e, options, nestedErrors);
nestedErrors.forEach(function (err) {
if (isArray(value) && err.property === property) {
err.property = (property ? property + '.' : '') + i;
} else {
err.property = (property ? property + '.' : '') + i + (err.property ? '.' + err.property.replace(property + '.', '') : '');
}
});
nestedErrors.unshift(errors.length, 0);
Array.prototype.splice.apply(errors, nestedErrors);
}
return true;
});
constrain('minItems', value, function (a, e) { return a.length >= e; });
constrain('maxItems', value, function (a, e) { return a.length <= e; });
constrain('uniqueItems', value, function (a, e) {
if (!e) {
return true;
}
var h = {};
for (var i = 0, l = a.length; i < l; i++) {
var key = JSON.stringify(a[i]);
if (h[key]) {
return false;
}
h[key] = true;
}
return true;
});
break;
case 'object':
// Recursive validation
if (schema.properties || schema.patternProperties || schema.additionalProperties) {
var nestedErrors = [];
validateObject(value, schema, options, nestedErrors);
nestedErrors.forEach(function (e) {
e.property = property + '.' + e.property;
});
nestedErrors.unshift(errors.length, 0);
Array.prototype.splice.apply(errors, nestedErrors);
}
break;
default:
break;
}
if (options.transform && typeof options.transform === 'function') {
options.transform({
object: object,
value: value,
property: property,
schema: schema,
options: options,
errors: errors
});
}
});
}
function checkType(val, type, callback) {
var types = isArray(type) ? type : [type];
// No type - no check
if (type === undefined) {
return callback(null, type);
}
// Go through available types
// And fine first matching
for (var i = 0, l = types.length; i < l; i++) {
type = types[i].trim();
// get function to check type
var typeCheckFunction = getTypeCheckFunction(type);
if (!typeCheckFunction) {
return callback(true);
}
if (typeCheckFunction(val)) {
return callback(null, type);
}
}
callback(true);
}
function error(attribute, property, actual, schema, errors) {
var lookup = { expected: schema[attribute], actual: actual, attribute: attribute, property: property };
var message = schema.messages && schema.messages[attribute] || schema.message || validate.messages[attribute] || 'no default message';
message = message.replace(/%\{([a-z]+)\}/ig, function (_, match) { return lookup[match.toLowerCase()] || ''; });
errors.push({
attribute: attribute,
property: property,
expected: schema[attribute],
actual: actual,
message: message
});
}
function isArray(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (typeof value.length === 'number' && !(value.propertyIsEnumerable('length')) &&
typeof value.splice === 'function') {
return true;
}
}
}
return false;
}
exports.validate = validate;
exports.mixin = mixin;
})
(typeof (window) === 'undefined' ? module.exports : (window.json = window.json || {}));
//noinspection JSUnresolvedVariable
(function (exports, revalidator, async) {
'use strict';
/**
* Extend revalidator format extensions
* @param extensionName
* @param extensionValue
*/
function extendFormatExtensions (extensionName, extensionValue) {
if (typeof extensionName !== 'string' || !(extensionValue instanceof RegExp)) {
throw new Error('extensionName or extensionValue undefined or not correct type');
}
if (revalidator.validate.formats.hasOwnProperty(extensionName) ||
revalidator.validate.formats.hasOwnProperty(extensionName)) {
var msg = 'extensionName: ' + extensionName + ' already exists in formatExtensions.';
throw new Error(msg);
}
revalidator.validate.formatExtensions[extensionName] = extensionValue;
}
/**
* Get Message from messages
* @param msgTyp
* @param replacement
* @return {String}
*/
function getMsg (msgTyp, replacement) {
return (revalidator.validate.messages[msgTyp] || '').replace('%{expected}', replacement);
}
/**
* Get an error object
* @param type
* @param expected
* @param actual
* @return {Object}
*/
function getError (type, expected, actual) {
return {
attribute: type,
expected: expected,
actual: actual,
message: getMsg(type, expected)
};
}
/**
* Get an validation result
* @param err
* @return {object}
*/
function getResult (err) {
var res = {
valid: true,
errors: []
};
if (err !== null) {
res.valid = false;
res.errors.push(err);
}
return res;
}
/**
* Check if array is unique
* @param val
* @return {Boolean}
*/
function uniqueArrayHelper (val) {
var h = {};
for (var i = 0, l = val.length; i < l; i++) {
var key = JSON.stringify(val[i]);
if (h[key]) {
return false;
}
h[key] = true;
}
return true;
}
/**
* Check formats
* @return {Object}
*/
function formats () {
var pub = {};
pub.email = function (val) {
if (!revalidator.validate.formats.email.test(val)) {
return getResult(getError('format', 'email', val));
}
return getResult(null);
};
pub.ipAddress = function (val) {
if (!revalidator.validate.formats['ip-address'].test(val)) {
return getResult(getError('format', 'ip-address', val));
}
return getResult(null);
};
pub.ipv6 = function (val) {
if (!revalidator.validate.formats.ipv6.test(val)) {
return getResult(getError('format', 'ipv6', val));
}
return getResult(null);
};
pub.dateTime = function (val) {
if (!revalidator.validate.formats['date-time'].test(val)) {
return getResult(getError('format', 'date-time', val));
}
return getResult(null);
};
pub.date = function (val) {
if (!revalidator.validate.formats.date.test(val)) {
return getResult(getError('format', 'date', val));
}
return getResult(null);
};
pub.time = function (val) {
if (!revalidator.validate.formats.time.test(val)) {
return getResult(getError('format', 'time', val));
}
return getResult(null);
};
pub.color = function (val) {
if (!revalidator.validate.formats.color.test(val)) {
return getResult(getError('format', 'color', val));
}
return getResult(null);
};
pub.hostName = function (val) {
if (!revalidator.validate.formats['host-name'].test(val)) {
return getResult(getError('format', 'host-name', val));
}
return getResult(null);
};
pub.utcMillisec = function (val) {
if (!revalidator.validate.formats['utc-millisec'].test(val)) {
return getResult(getError('format', 'utc-millisec', val));
}
return getResult(null);
};
pub.regex = function (val) {
if (!(val instanceof RegExp)) {
return getResult(getError('format', 'regex', val));
}
return getResult(null);
};
pub.url = function (val) {
if (!revalidator.validate.formatExtensions.url.test(val)) {
return getResult(getError('format', 'url', val));
}
return getResult(null);
};
pub.mongoId = function (val) {
if (!revalidator.validate.formats['mongo-id'].test(val)) {
return getResult(getError('format', 'mongoId', val));
}
return getResult(null);
};
pub.uuid = function (val) {
if (!revalidator.validate.formats['uuid'].test(val)) {
return getResult(getError('format', 'uuid', val));
}
return getResult(null);
};
pub.luuid = function (val) {
if (!revalidator.validate.formats['luuid'].test(val)) {
return getResult(getError('format', 'luuid', val));
}
return getResult(null);