-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.js
396 lines (369 loc) · 12.3 KB
/
generator.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
(function(){
"use strict";
var _ = require("underscore");
var RandExp = require("randexp")
var randexp = RandExp.randexp;
RandExp.prototype.anyRandChar = function(){
var characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ ";
return characters[_.random(characters.length-1)];
};
var defaultMax = RandExp.prototype.max = 10;
// All of these functions assume that the schema is valid and consistent
var gen = {};
// Generates an instance of a schema that has an enum property.
var gen_enum = function(schema){
return schema["enum"][_.random(schema["enum"].length)];
};
var genFormat = function(format){
switch(format){
case "date-time" : return new Date(_.random(100000000000000)).toISOString();
case "email" : return randexp(/\w+@example\.com/);
case "hostname" : return randexp(/\w+\.example\.com/);
case "ipv4" : return _.map([1,2,3,4], function(){return _.random(255).toString();}).join('.');
case "ipv6" : return randexp(/[abcdef\d]{4}(:[abcdef\d]{4}){7}/);
case "uri" : return randexp(/\w+\.example\.com\/\w+/);
case "phone" : return randexp(/\d{3}-\d{3}-\d{4}/);
default : return randexp(/.*/);
}
};
// Generates an instance of a schema of type array
gen["array"] = function(schema){
var maxItems = schema.maxItems;
var minItems = schema.minItems;
var instance = [];
var amount;
var items;
var i;
var newITem;
if(_.has(schema, "items")){
items = schema.items;
if(_.isArray(items)){
amount = _.random(minItems+1, Math.min(maxItems, items.length));
instance = _.map(items.slice(0, amount), function(subschema){
return generate(subschema);
});
while(instance.length < minItems){
if(_.isObject(schema.additionalItems)){
newItem = generate(schema.additionalItems);
} else {
newItem = null;
}
instance = instance.concat(newItem);
}
} else {
// typeof(items) === "object"
amount = _.random(minItems+1, maxItems);
for(i=0; i<amount; i++){
instance[i] = generate(items);
}
}
}
return instance;
};
gen["boolean"] = function(schema){
return _.random(1) > 0;
};
gen["integer"] = function(schema){
var min = schema.minimum;
var max = schema.maximum;
var multipleOf = schema.multipleOf;
return multipleOf * _.random(min, max);
};
gen["number"] = function(schema){
var value = gen["integer"](schema);
if(value>=schema.maximum){
value = schema.maximum - 1;
}
return value + Math.random();
};
gen["null"] = function(schema){
return null;
};
gen["object"] = function(schema){
var remaining = _.keys(schema.properties);
var instance = {};
var min = schema.minProperties;
var max = schema.maxProperties;
_.each(schema.required, function(name){
instance[name] = generate(schema.properties[name]);
});
remaining = _.difference(remaining, schema.required);
var amount = _.random(Math.max(min, schema.required.length), max) - schema.required.length;
_.each(remaining.slice(0, amount), function(name){
instance[name] = generate(schema.properties[name]);
});
return instance;
};
gen["string"] = function(schema){
var min = Math.max(1, schema.minLength);
var max = schema.maxLength;
if(_.has(schema, "pattern")){
return randexp(schema.pattern);
}
if(_.has(schema, "format")){
return genFormat(schema.format);
}
return randexp('.{'+min+','+max+'}');
};
function choose(schema){
if(_.has(schema, "oneOf")){
return mergeSchemas(schema, schema.oneOf[_.random(schema.oneOf.length-1)]);
} else {
return schema;
}
}
function generate(schema){
var choice = choose(schema);
if(_.isEmpty(choice.type)){
return null;
} else {
return gen[choice.type[_.random(choice.type.length-1)]](choice);
}
};
function deepEqual(A, B){
if(_.isArray(A) && _.isArray(B)){
return _.every(_.zip(A, B), function(pair){
return deepEqual(pair[0], pair[1]);
});
} else if(_.isObject(A) && _.isObject(B)){
return deepEqual(_.pairs(A), _.pairs(B));
} else {
return A === B;
}
};
function combineProperties(source, dest, prop, combiner, def){
if(_.has(source, prop)){
if(_.has(dest, prop)){
return combiner(source[prop], dest[prop], source, dest);
} else {
return source[prop];
}
} else {
if(_.has(dest, prop)){
return dest[prop];
} else {
return def;
}
}
};
function mergeSchemas(dest, source){
var result = {};
var destAdditionalProps = normalize(_.isObject(dest.additionalProperties) ? dest.additionalProperties : {});
var srcAdditionalProps = normalize(_.isObject(source.additionalProperties) ? source.additionalProperties : {});
result.multipleOf = combineProperties(source, dest, "multipleOf", function(a,b){return a*b;}, 1);
result.maximum = combineProperties(source, dest, "maximum", Math.min, defaultMax);
result.minimum = combineProperties(source, dest, "maximum", Math.max, -defaultMax);
result.maxLength = Math.min(dest.maxLength, source.maxLength);
result.minLength = Math.max(dest.minLength, source.minLength);
if(_.has(source, "pattern")){
if(_.has(dest, "pattern")){
result.pattern = source.pattern + dest.pattern;
} else {
result.pattern = source.pattern;
}
} else if(_.has(dest, "pattern")){
result.pattern = dest.pattern;
}
if(_.has(source, "format")){
if(_.has(dest, "format")){
if(source.format === dest.format){
result.format = source.format;
}
} else {
result.format = source.format;
}
} else {
if(_.has(dest, "format")){
result.format = dest.format;
}
}
result.maxItems = Math.min(dest.maxItems, source.maxItems);
result.minItems = Math.max(dest.minItems, source.minItems);
if(_.has(source, "items")){
if(_.has(dest, "items")){
if(_.isArray(source.items)){
if(_.isArray(dest.items)){
while(dest.items.length < source.items.length){
if(_.isObject(dest.additionalItems)){
dest.items = dest.items.concat(normalize(dest.additionalItems));
} else {
dest.items = dest.items.concat({});
}
}
while(source.items.length < dest.items.length){
if(_.isObject(source.additionalItems)){
source.items = source.items.concat(normalize(dest.additionalItems));
} else {
source.items = source.items.concat({});
}
}
result.items = _.map(_.zip(source.items, dest.items), function(pair){
result.items = mergeSchemas(pair[0], pair[1]);
});
} else {
result.items = _.map(source.items, function(item){
return mergeSchemas(item, dest.items);
});
}
} else {
if(_.isArray(dest.items)){
result.items = _.map(dest.items, function(item){
return mergeSchemas(source.items, item);
});
} else {
result.items = mergeSchemas(dest.items, source.items);
}
}
} else {
result.items = source.items;
}
} else {
if(_.has(dest, "items")){
result.items = dest.items;
}
}
if(source.additionalItems && dest.additionalItems){
if(_.isObject(source.additionalItems)){
if(_.isObject(dest.additionalItems)){
result.additionalItems = mergeSchemas(source.additionalItems, dest.additionalItems);
} else {
result.additionalItems = source.additionalItems;
}
} else {
result.additionalItems = source.additionalItems && dest.additionalItems;
}
}
result.uniqueItems = source.uniqueItems || dest.uniqueItems;
if(_.has(source, "properties")){
if(_.has(dest, "properties")){
result.properties = {};
_.each(_.intersection(_.keys(source.properties), _.keys(dest.properties)), function(name){
result.properties[name] = mergeSchemas(source.properties[name], dest.properties[name]);
});
_.each(_.difference(_.keys(source.properties), _.keys(dest.properties)), function(name){
result.properties[name] = mergeSchemas(source.properties[name], normalize(destAdditionalProps));
});
_.each(_.difference(_.keys(dest.properties), _.keys(source.properties)), function(name){
result.properties[name] = mergeSchemas(dest.properties[name], normalize(srcAdditionalProps));
});
} else {
result.properties = source.properties;
}
} else {
if(_.has(dest, "properties")){
result.properties = dest.properties
}
}
result.maxProperties = Math.min(source.maxProperties, dest.maxProperties);
result.minProperties = Math.max(source.minProperties, dest.minProperties);
result.required = _.union(source.required, dest.required);
if(_.has(source, "enum")){
if(_.has(dest, "enum")){
result["enum"] = _.filter(source["enum"], function(item){
return _.some(dest["enum"], _.partial(deepEqual, item));
});
} else {
result["enum"] = source["enum"];
}
} else {
if(_.has(dest, "enum")){
result["enum"] = dest["enum"];
}
}
//allOf
if(_.has(source, "allOf")){
if(_.has(dest, "allOf")){
result.allOf = _.union(source.allOf, dest.allOf);
} else {
result.allOf = source.allOf;
}
} else {
if(_.has(dest, "allOf")){
result.allOf = dest.allOf;
}
}
//oneOf
//naive, doesn't take intersection into account
if(_.has(source, "oneOf")){
if(_.has(dest, "oneOf")){
result.oneOf = _.flatten(_.map(source.oneOf, function(subschema){
return _.map(dest.oneOf, function(item){
return mergeSchemas(subschema, item);
});
}));
} else {
result.oneOf = source.oneOf;
}
} else {
if(_.has(dest, "oneOf")){
result.oneOf = dest.oneOf;
}
}
result.type = _.intersection(source.type, dest.type);
return result;
}
function normalize(schema){
var allOf;
var defaultSchema = {
multipleOf : 1,
maximum : defaultMax,
minimum : 0,
exclusiveMaximum : false,
exclusiveMinimum : false,
maxLength : defaultMax,
minLength : 0,
items : [],
additionalItems : true,
maxItems : defaultMax,
minItems : 0,
uniqueItems : false,
properties : {},
maxProperties : Infinity,
minProperties : 0,
required : [],
type : ["array", "boolean", "integer", "number", "null", "object", "string"]
};
if(_.isString(schema.type)){
schema.type = [schema.type];
}
if(_.contains(schema.type, "number")){
schema.type = _.union(schema.type, ["integer"]);
}
if(schema.exclusiveMaximum){
schema.exclusiveMaximum = false;
schema.maximum -= 1;
}
if(schema.exclusiveMinimum){
schema.exclusiveMinimum = false;
schema.minimum += 1;
}
if(schema.additionalProperties === true){
schema.additionalProperties = {};
}
if(_.has(schema, "oneOf")){
schema.oneOf = _.map(schema.oneOf, normalize);
}
schema.properties = _.object(_.map(schema.properties, function(value, name){
return [name, normalize(value)]
}));
schema.patternProperties = _.object(_.map(schema.patternProperties, function(value, name){
return [name, normalize(value)]
}));
if(_.isObject(schema.additionalProperties)){
schema.additionalProperties = normalize(schema.additionalProperties);
}
schema.items = _.map(schema.items, normalize);
if(_.isObject(schema.additionalItems)){
schema.additionalItems = normalize(schema.additionalItems);
}
schema = _.defaults(schema, defaultSchema);
allOf = _.clone(schema.allOf);
delete schema.allOf;
schema = _.reduce(_.map(allOf, normalize), mergeSchemas, schema);
return schema;
};
module.exports = function(schema){
var norm = normalize(schema);
this.generate = _.partial(generate, norm);
};
}());