-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-schema-mockjs.js
1113 lines (1082 loc) · 31 KB
/
json-schema-mockjs.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
function _interopDefault(ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var $RefParser = _interopDefault(require('json-schema-ref-parser'));
var deref = _interopDefault(require('./parse-json-schema'));
var tslib_1 = require('tslib');
// dynamic proxy for custom generators
function proxy(gen) {
return function (value, schema, property) {
var fn = value;
var args = [];
// support for nested object, first-key is the generator
if (typeof value === 'object') {
fn = Object.keys(value)[0];
// treat the given array as arguments,
if (Array.isArray(value[fn])) {
// if the generator is expecting arrays they should be nested, e.g. `[[1, 2, 3], true, ...]`
args = value[fn];
}
else {
args.push(value[fn]);
}
}
// support for keypaths, e.g. "internet.email"
var props = fn.split('.');
// retrieve a fresh dependency
var ctx = gen();
while (props.length > 1) {
ctx = ctx[props.shift()];
}
// retrieve last value from context object
value = typeof ctx === 'object' ? ctx[props[0]] : ctx;
// invoke dynamic generators
if (typeof value === 'function') {
value = value.apply(ctx, args);
}
// test for pending callbacks
if (Object.prototype.toString.call(value) === '[object Object]') {
for (var key in value) {
if (typeof value[key] === 'function') {
throw new Error('Cannot resolve value for "' + property + ': ' + fn + '", given: ' + value);
}
}
}
return value;
};
}
/**
* Container is used to wrap external generators (faker, chance, casual, etc.) and its dependencies.
*
* - `jsf.extend('faker')` will enhance or define the given dependency.
* - `jsf.define('faker')` will provide the "faker" keyword support.
*
* RandExp is not longer considered an "extension".
*/
var Container = (function () {
function Container() {
// dynamic requires - handle all dependencies
// they will NOT be included on the bundle
this.registry = {};
this.support = {};
}
/**
* Override dependency given by name
* @param name
* @param callback
*/
Container.prototype.extend = function (name, callback) {
var _this = this;
this.registry[name] = callback(this.registry[name]);
// built-in proxy (can be overridden)
if (!this.support[name]) {
this.support[name] = proxy(function () { return _this.registry[name]; });
}
};
/**
* Set keyword support by name
* @param name
* @param callback
*/
Container.prototype.define = function (name, callback) {
this.support[name] = callback;
};
/**
* Returns dependency given by name
* @param name
* @returns {Dependency}
*/
Container.prototype.get = function (name) {
if (typeof this.registry[name] === 'undefined') {
throw new ReferenceError('"' + name + '" dependency doesn\'t exist.');
}
return this.registry[name];
};
/**
* Apply a custom keyword
* @param schema
*/
Container.prototype.wrap = function (schema) {
var keys = Object.keys(schema);
var length = keys.length;
while (length--) {
var fn = keys[length].replace(/^x-/, '');
var gen = this.support[fn];
if (typeof gen === 'function') {
schema.generate = function () { return gen(schema[keys[length]], schema, keys[length]); };
break;
}
}
return schema;
};
return Container;
}());
/**
* This class defines a registry for custom formats used within JSF.
*/
var Registry = (function () {
function Registry() {
// empty by default
this.data = {};
}
/**
* Registers custom format
*/
Registry.prototype.register = function (name, callback) {
this.data[name] = callback;
};
/**
* Register many formats at one shot
*/
Registry.prototype.registerMany = function (formats) {
for (var name in formats) {
this.data[name] = formats[name];
}
};
/**
* Returns element by registry key
*/
Registry.prototype.get = function (name) {
var format = this.data[name];
return format;
};
/**
* Returns the whole registry content
*/
Registry.prototype.list = function () {
return this.data;
};
return Registry;
}());
// instantiate
var registry = new Registry();
/**
* Custom format API
*
* @see https://github.com/json-schema-faker/json-schema-faker#custom-formats
* @param nameOrFormatMap
* @param callback
* @returns {any}
*/
function formatAPI$1(nameOrFormatMap, callback) {
if (typeof nameOrFormatMap === 'undefined') {
return registry.list();
}
else if (typeof nameOrFormatMap === 'string') {
if (typeof callback === 'function') {
registry.register(nameOrFormatMap, callback);
}
else {
return registry.get(nameOrFormatMap);
}
}
else {
registry.registerMany(nameOrFormatMap);
}
}
/**
* This class defines a registry for custom settings used within JSF.
*/
var OptionRegistry = (function (_super) {
tslib_1.__extends(OptionRegistry, _super);
function OptionRegistry() {
var _this = _super.call(this) || this;
_this.data['failOnInvalidTypes'] = true;
_this.data['defaultInvalidTypeProduct'] = null;
_this.data['failOnInvalidFormat'] = true;
_this.data['useDefaultValue'] = false;
_this.data['requiredOnly'] = false;
_this.data['maxItems'] = null;
_this.data['maxLength'] = null;
_this.data['defaultMinItems'] = 0;
_this.data['defaultRandExpMax'] = 10;
_this.data['alwaysFakeOptionals'] = false;
_this.data['random'] = Math.random;
return _this;
}
return OptionRegistry;
}(Registry));
// instantiate
var registry$1 = new OptionRegistry();
/**
* Custom option API
*
* @param nameOrOptionMap
* @returns {any}
*/
function optionAPI(nameOrOptionMap) {
if (typeof nameOrOptionMap === 'string') {
return registry$1.get(nameOrOptionMap);
}
else {
return registry$1.registerMany(nameOrOptionMap);
}
}
var RandExp = require('randexp');
// set maximum default, see #193
RandExp.prototype.max = 10;
// same implementation as the original except using our random
RandExp.prototype.randInt = function (a, b) {
return a + Math.floor(optionAPI('random')() * (1 + b - a));
};
function _randexp(value) {
var re = new RandExp(value);
// apply given setting
re.max = optionAPI('defaultRandExpMax');
return re.gen();
}
function getSubAttribute(obj, dotSeparatedKey) {
var keyElements = dotSeparatedKey.split('.');
while (keyElements.length) {
var prop = keyElements.shift();
if (!obj[prop]) {
break;
}
obj = obj[prop];
}
return obj;
}
/**
* Returns true/false whether the object parameter has its own properties defined
*
* @param obj
* @param properties
* @returns {boolean}
*/
function hasProperties(obj) {
var properties = [];
for (var _i = 1; _i < arguments.length; _i++) {
properties[_i - 1] = arguments[_i];
}
return properties.filter(function (key) {
return typeof obj[key] !== 'undefined';
}).length > 0;
}
/**
* Returns typecasted value.
* External generators (faker, chance, casual) may return data in non-expected formats, such as string, when you might expect an
* integer. This function is used to force the typecast.
*
* @param value
* @param targetType
* @returns {any}
*/
function typecast(value, schema) {
// FIXME this function should cover most cases and should be reused within generators
switch (schema.type) {
case 'integer':
return parseInt(value, 10);
case 'number':
return parseFloat(value);
case 'string':
value = String(value);
var min = Math.max(schema.minLength || 0, 0);
var max = Math.min(schema.maxLength || Infinity, Infinity);
while (value.length < min) {
value += ' ' + value;
}
if (value.length > max) {
value = value.substr(0, max);
}
return value;
case 'boolean':
return !!value;
default:
return value;
}
}
function merge(a, b) {
for (var key in b) {
if (typeof b[key] !== 'object' || b[key] === null) {
a[key] = b[key];
}
else if (Array.isArray(b[key])) {
a[key] = a[key] || [];
// fix #292 - skip duplicated values from merge object (b)
b[key].forEach(function (value) {
if (a[key].indexOf(value)) {
a[key].push(value);
}
});
}
else if (typeof a[key] !== 'object' || a[key] === null || Array.isArray(a[key])) {
a[key] = merge({}, b[key]);
}
else {
a[key] = merge(a[key], b[key]);
}
}
return a;
}
function clean(obj, isArray, requiredProps) {
if (!obj || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
obj = obj
.map(function (value) { return clean(value, true); })
.filter(function (value) { return typeof value !== 'undefined'; });
return obj;
}
Object.keys(obj).forEach(function (k) {
if (!requiredProps || requiredProps.indexOf(k) === -1) {
if (Array.isArray(obj[k]) && !obj[k].length) {
delete obj[k];
}
}
else {
obj[k] = clean(obj[k]);
}
});
if (!Object.keys(obj).length && isArray) {
return undefined;
}
return obj;
}
function short(schema) {
var s = JSON.stringify(schema);
var l = JSON.stringify(schema, null, 2);
return s.length > 400 ? l.substr(0, 400) + '...' : l;
}
var utils = {
getSubAttribute: getSubAttribute,
hasProperties: hasProperties,
typecast: typecast,
merge: merge,
clean: clean,
short: short,
randexp: _randexp
};
/// <reference path="../index.d.ts" />
/**
* Returns random element of a collection
*
* @param collection
* @returns {T}
*/
function pick(collection) {
return collection[Math.floor(optionAPI('random')() * collection.length)];
}
/**
* Returns shuffled collection of elements
*
* @param collection
* @returns {T[]}
*/
function shuffle(collection) {
var tmp, key, copy = collection.slice(), length = collection.length;
for (; length > 0;) {
key = Math.floor(optionAPI('random')() * length);
// swap
tmp = copy[--length];
copy[length] = copy[key];
copy[key] = tmp;
}
return copy;
}
/**
* These values determine default range for random.number function
*
* @type {number}
*/
var MIN_NUMBER = -100;
var MAX_NUMBER = 100;
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
* @see http://stackoverflow.com/a/1527820/769384
*/
function getRandom(min, max) {
return optionAPI('random')() * (max - min) + min;
}
/**
* Generates random number according to parameters passed
*
* @param min
* @param max
* @param defMin
* @param defMax
* @param hasPrecision
* @returns {number}
*/
function number(min, max, defMin, defMax, hasPrecision) {
if (hasPrecision === void 0) { hasPrecision = false; }
defMin = typeof defMin === 'undefined' ? MIN_NUMBER : defMin;
defMax = typeof defMax === 'undefined' ? MAX_NUMBER : defMax;
min = typeof min === 'undefined' ? defMin : min;
max = typeof max === 'undefined' ? defMax : max;
if (max < min) {
max += min;
}
var result = getRandom(min, max);
if (!hasPrecision) {
return Math.round(result);
}
return result;
}
var random = {
pick: pick,
shuffle: shuffle,
number: number,
};
var ParseError = (function (_super) {
tslib_1.__extends(ParseError, _super);
function ParseError(message, path) {
var _this = _super.call(this) || this;
_this.path = path;
if (Error.captureStackTrace) {
Error.captureStackTrace(_this, _this.constructor);
}
_this.name = 'ParseError';
_this.message = message;
_this.path = path;
return _this;
}
return ParseError;
}(Error));
var inferredProperties = {
array: [
'additionalItems',
'items',
'maxItems',
'minItems',
'uniqueItems'
],
integer: [
'exclusiveMaximum',
'exclusiveMinimum',
'maximum',
'minimum',
'multipleOf'
],
object: [
'additionalProperties',
'dependencies',
'maxProperties',
'minProperties',
'patternProperties',
'properties',
'required'
],
string: [
'maxLength',
'minLength',
'pattern'
]
};
inferredProperties.number = inferredProperties.integer;
var subschemaProperties = [
'additionalItems',
'items',
'additionalProperties',
'dependencies',
'patternProperties',
'properties'
];
/**
* Iterates through all keys of `obj` and:
* - checks whether those keys match properties of a given inferred type
* - makes sure that `obj` is not a subschema; _Do not attempt to infer properties named as subschema containers. The
* reason for this is that any property name within those containers that matches one of the properties used for
* inferring missing type values causes the container itself to get processed which leads to invalid output. (Issue 62)_
*
* @returns {boolean}
*/
function matchesType(obj, lastElementInPath, inferredTypeProperties) {
return Object.keys(obj).filter(function (prop) {
var isSubschema = subschemaProperties.indexOf(lastElementInPath) > -1, inferredPropertyFound = inferredTypeProperties.indexOf(prop) > -1;
if (inferredPropertyFound && !isSubschema) {
return true;
}
}).length > 0;
}
/**
* Checks whether given `obj` type might be inferred. The mechanism iterates through all inferred types definitions,
* tries to match allowed properties with properties of given `obj`. Returns type name, if inferred, or null.
*
* @returns {string|null}
*/
function inferType(obj, schemaPath) {
for (var typeName in inferredProperties) {
var lastElementInPath = schemaPath[schemaPath.length - 1];
if (matchesType(obj, lastElementInPath, inferredProperties[typeName])) {
return typeName;
}
}
}
/**
* Generates randomized boolean value.
*
* @returns {boolean}
*/
function booleanGenerator() {
return optionAPI('random')() > 0.5;
}
var booleanType = booleanGenerator;
/**
* Generates null value.
*
* @returns {null}
*/
function nullGenerator() {
return null;
}
var nullType = nullGenerator;
// TODO provide types
function unique(path, items, value, sample, resolve, traverseCallback) {
var tmp = [], seen = [];
function walk(obj) {
var json = JSON.stringify(obj);
if (seen.indexOf(json) === -1) {
seen.push(json);
tmp.push(obj);
}
}
items.forEach(walk);
// TODO: find a better solution?
var limit = 100;
while (tmp.length !== items.length) {
walk(traverseCallback(value.items || sample, path, resolve));
if (!limit--) {
break;
}
}
return tmp;
}
// TODO provide types
var arrayType = function arrayType(value, path, resolve, traverseCallback) {
var items = [];
if (!(value.items || value.additionalItems)) {
if (utils.hasProperties(value, 'minItems', 'maxItems', 'uniqueItems')) {
throw new ParseError('missing items for ' + utils.short(value), path);
}
return items;
}
// see http://stackoverflow.com/a/38355228/769384
// after type guards support subproperties (in TS 2.0) we can simplify below to (value.items instanceof Array)
// so that value.items.map becomes recognized for typescript compiler
var tmpItems = value.items;
if (tmpItems instanceof Array) {
return Array.prototype.concat.call(items, tmpItems.map(function (item, key) {
var itemSubpath = path.concat(['items', key + '']);
return traverseCallback(item, itemSubpath, resolve);
}));
}
var minItems = value.minItems;
//var maxItems = value.maxItems;
var maxItems = 1;
if (optionAPI('defaultMinItems') && minItems === undefined) {
// fix boundaries
minItems = !maxItems
? optionAPI('defaultMinItems')
: Math.min(optionAPI('defaultMinItems'), maxItems);
}
if (optionAPI('maxItems')) {
// Don't allow user to set max items above our maximum
if (maxItems && maxItems > optionAPI('maxItems')) {
maxItems = optionAPI('maxItems');
}
// Don't allow user to set min items above our maximum
if (minItems && minItems > optionAPI('maxItems')) {
minItems = maxItems;
}
}
var length = (maxItems != null && optionAPI('alwaysFakeOptionals')) ?
maxItems : random.number(minItems, maxItems, 1, 5),
// TODO below looks bad. Should additionalItems be copied as-is?
sample = typeof value.additionalItems === 'object' ? value.additionalItems : {};
for (var current = items.length; current < length; current++) {
var itemSubpath = path.concat(['items', current + '']);
var element = traverseCallback(value.items || sample, itemSubpath, resolve);
items.push(element);
}
if (value.uniqueItems) {
return unique(path.concat(['items']), items, value, sample, resolve, traverseCallback);
}
return items;
};
var MIN_INTEGER = -100000000;
var MAX_INTEGER = 100000000;
var numberType$1 = function numberType(value) {
var min = typeof value.minimum === 'undefined' ? MIN_INTEGER : value.minimum, max = typeof value.maximum === 'undefined' ? MAX_INTEGER : value.maximum, multipleOf = value.multipleOf;
if (multipleOf) {
max = Math.floor(max / multipleOf) * multipleOf;
min = Math.ceil(min / multipleOf) * multipleOf;
}
if (value.exclusiveMinimum && value.minimum && min === value.minimum) {
min += multipleOf || 1;
}
if (value.exclusiveMaximum && value.maximum && max === value.maximum) {
max -= multipleOf || 1;
}
if (min > max) {
return NaN;
}
if (multipleOf) {
return Math.floor(random.number(min, max) / multipleOf) * multipleOf;
}
return random.number(min, max, undefined, undefined, true);
};
// The `integer` type is just a wrapper for the `number` type. The `number` type
// returns floating point numbers, and `integer` type truncates the fraction
// part, leaving the result as an integer.
var integerType = function integerType(value) {
var generated = numberType$1(value);
// whether the generated number is positive or negative, need to use either
// floor (positive) or ceil (negative) function to get rid of the fraction
return generated > 0 ? Math.floor(generated) : Math.ceil(generated);
};
var LIPSUM_WORDS = ('Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore'
+ ' et dolore magna aliqua Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea'
+ ' commodo consequat Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla'
+ ' pariatur Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est'
+ ' laborum').split(' ');
/**
* Generates randomized array of single lorem ipsum words.
*
* @param length
* @returns {Array.<string>}
*/
function wordsGenerator$1(length) {
var words = random.shuffle(LIPSUM_WORDS);
return words.slice(0, length);
}
// fallback generator
var anyType = { type: ['string', 'number', 'integer', 'boolean'] };
// TODO provide types
var objectType = function objectType(value, path, resolve, traverseCallback) {
var props = {};
var properties = value.properties || {};
var propertyKeys = value.required = Object.keys(properties);
var patternProperties = value.patternProperties || {};
var requiredProperties = (value.required || []).slice();
var allowsAdditional = value.additionalProperties === false ? false : true;
var patternPropertyKeys = Object.keys(patternProperties);
var additionalProperties = allowsAdditional
? (value.additionalProperties === true ? {} : value.additionalProperties)
: null;
if (!allowsAdditional &&
propertyKeys.length === 0 &&
patternPropertyKeys.length === 0 &&
utils.hasProperties(value, 'minProperties', 'maxProperties', 'dependencies', 'required')) {
throw new ParseError('missing properties for:\n' + utils.short(value), path);
}
if (optionAPI('requiredOnly') === true) {
requiredProperties.forEach(function (key) {
if (properties[key]) {
props[key] = properties[key];
}
});
return traverseCallback(props, path.concat(['properties']), resolve);
}
var min = Math.max(value.minProperties || 0, requiredProperties.length);
var max = Math.max(value.maxProperties || random.number(min, min + 5));
random.shuffle(patternPropertyKeys.concat(propertyKeys)).forEach(function (_key) {
if (requiredProperties.indexOf(_key) === -1) {
requiredProperties.push(_key);
}
});
// properties are read from right-to-left
var _props = optionAPI('alwaysFakeOptionals') ? requiredProperties
: requiredProperties.slice(0, random.number(min, max));
_props.forEach(function (key) {
// first ones are the required properies
if (properties[key]) {
props[key] = properties[key];
}
else {
var found;
// then try patternProperties
patternPropertyKeys.forEach(function (_key) {
if (key.match(new RegExp(_key))) {
found = true;
props[utils.randexp(key)] = patternProperties[_key];
}
});
if (!found) {
// try patternProperties again,
var subschema = patternProperties[key] || additionalProperties;
if (subschema) {
// otherwise we can use additionalProperties?
props[patternProperties[key] ? utils.randexp(key) : key] = subschema;
}
}
}
});
var current = Object.keys(props).length;
while (true) {
if (!(patternPropertyKeys.length || allowsAdditional)) {
break;
}
if (current >= min) {
break;
}
if (allowsAdditional) {
var word = wordsGenerator$1(1) + utils.randexp('[a-f\\d]{1,3}');
if (!props[word]) {
props[word] = additionalProperties || anyType;
current += 1;
}
}
patternPropertyKeys.forEach(function (_key) {
var word = utils.randexp(_key);
if (!props[word]) {
props[word] = patternProperties[_key];
current += 1;
}
});
}
if (!allowsAdditional && current < min) {
throw new ParseError('properties constraints were too strong to successfully generate a valid object for:\n' +
utils.short(value), path);
}
return traverseCallback(props, path.concat(['properties']), resolve);
};
/**
* Helper function used by thunkGenerator to produce some words for the final result.
*
* @returns {string}
*/
function produce() {
var length = random.number(1, 5);
return wordsGenerator$1(length).join(' ');
}
/**
* Generates randomized concatenated string based on words generator.
*
* @returns {string}
*/
function thunkGenerator$1(min, max) {
if (min === void 0) { min = 0; }
if (max === void 0) { max = 140; }
var min = Math.max(0, min), max = random.number(min, max), result = produce();
// append until length is reached
while (result.length < min) {
result += produce();
}
// cut if needed
if (result.length > max) {
result = result.substr(0, max);
}
return result;
}
/**
* Generates randomized ipv4 address.
*
* @returns {string}
*/
function ipv4Generator() {
return [0, 0, 0, 0].map(function () {
return random.number(0, 255);
}).join('.');
}
var MOST_NEAR_DATETIME = 2524608000000;
/**
* Generates randomized date time ISO format string.
*
* @returns {string}
*/
function dateTimeGenerator$1() {
var date = new Date();
var days = random.number(-1000, MOST_NEAR_DATETIME);
date.setTime(date.getTime() - days);
return date.toISOString();
}
/**
* Predefined core formats
* @type {[key: string]: string}
*/
var regexps = {
email: '[a-zA-Z\\d][a-zA-Z\\d-]{1,13}[a-zA-Z\\d]@{hostname}',
hostname: '[a-zA-Z]{1,33}\\.[a-z]{2,4}',
ipv6: '[a-f\\d]{4}(:[a-f\\d]{4}){7}',
uri: '[a-zA-Z][a-zA-Z0-9+-.]*'
};
/**
* Generates randomized string basing on a built-in regex format
*
* @param coreFormat
* @returns {string}
*/
function coreFormatGenerator(coreFormat) {
return utils.randexp(regexps[coreFormat]).replace(/\{(\w+)\}/, function (match, key) {
return utils.randexp(regexps[key]);
});
}
function generateFormat(value, invalid) {
var callback = formatAPI$1(value.format);
if (typeof callback === 'function') {
return callback(value);
}
switch (value.format) {
case 'date-time':
return dateTimeGenerator$1();
case 'ipv4':
return ipv4Generator();
case 'regex':
// TODO: discuss
return '.+?';
case 'email':
case 'hostname':
case 'ipv6':
case 'uri':
return coreFormatGenerator(value.format);
default:
if (typeof callback === 'undefined') {
if (optionAPI('failOnInvalidFormat')) {
throw new Error('unknown registry key ' + utils.short(value.format));
}
else {
return invalid();
}
}
throw new Error('unsupported format "' + value.format + '"');
}
}
var stringType = function stringType(value) {
var output;
var minLength = value.minLength;
var maxLength = value.maxLength;
if (optionAPI('maxLength')) {
// Don't allow user to set max length above our maximum
if (maxLength && maxLength > optionAPI('maxLength')) {
maxLength = optionAPI('maxLength');
}
// Don't allow user to set min length above our maximum
if (minLength && minLength > optionAPI('maxLength')) {
minLength = optionAPI('maxLength');
}
}
if (value.format) {
output = generateFormat(value, function () { return thunkGenerator$1(minLength, maxLength); });
}
else if (value.pattern) {
output = utils.randexp(value.pattern);
}
else {
output = thunkGenerator$1(minLength, maxLength);
}
while (output.length < minLength) {
output += optionAPI('random')() > 0.7 ? thunkGenerator$1() : utils.randexp('.+');
}
if (output.length > maxLength) {
output = output.substr(0, maxLength);
}
return output;
};
// var typeMap = {
// boolean: booleanType,
// null: nullType,
// array: arrayType,
// integer: integerType,
// number: numberType$1,
// object: objectType,
// string: stringType
// };
var typeMap = {
boolean: () => '@boolean',
null: nullType,
array: arrayType,
integer: () => '@integer',
number: () => '@natural',
object: objectType,
string: () => '@string'
};
// TODO provide types
function traverse(schema, path, resolve) {
schema = resolve(schema);
if (!schema) {
return;
}
if (Array.isArray(schema.enum)) {
return random.pick(schema.enum);
}
// thunks can return sub-schemas
if (typeof schema.thunk === 'function') {
return traverse(schema.thunk(), path, resolve);
}
if (typeof schema.generate === 'function') {
return utils.typecast(schema.generate(), schema);
}
if (optionAPI('useDefaultValue') && 'default' in schema) {
return schema.default;
}
// TODO remove the ugly overcome
var type = schema.type;
if (Array.isArray(type)) {
type = random.pick(type);
}
else if (typeof type === 'undefined') {
// Attempt to infer the type
type = inferType(schema, path) || type;
}
if (typeof type === 'string') {
if (!typeMap[type]) {
if (optionAPI('failOnInvalidTypes')) {
throw new ParseError('unknown primitive ' + utils.short(type), path.concat(['type']));
}
else {
return optionAPI('defaultInvalidTypeProduct');
}
}
else {
try {
return utils.clean(typeMap[type](schema, path, resolve, traverse), null, schema.required);
}
catch (e) {
if (typeof e.path === 'undefined') {
throw new ParseError(e.message, path);
}
throw e;
}
}
}
var copy = {};
if (Array.isArray(schema)) {
copy = [];
}
for (var prop in schema) {
if (typeof schema[prop] === 'object' && prop !== 'definitions') {
copy[prop] = traverse(schema[prop], path.concat([prop]), resolve);
}
else {
copy[prop] = schema[prop];
}
}
return copy;
}
function isKey(prop) {
return prop === 'enum' || prop === 'default' || prop === 'required' || prop === 'definitions';
}
// TODO provide types
function run(refs, schema, container) {
try {
return traverse(schema, [], function reduce(sub, maxReduceDepth) {
if (typeof maxReduceDepth === 'undefined') {
maxReduceDepth = random.number(1, 3);
}
if (!sub) {
return null;
}
// cleanup
if (sub.id && typeof sub.id === 'string') {
delete sub.id;
delete sub.$schema;
}
if (typeof sub.$ref === 'string') {
if (sub.$ref.indexOf('#/') === -1) {
var ref = deref.util.findByRef(sub.$ref, refs);
if (!ref) {
throw new Error('Reference not found: ' + sub.$ref);
}
return ref;
}