This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathschema.js
More file actions
395 lines (321 loc) · 12.5 KB
/
schema.js
File metadata and controls
395 lines (321 loc) · 12.5 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
/* eslint-disable class-methods-use-this, arrow-body-style */
const _ = require('lodash');
const { parseReference, lookupReference, dereference } = require('./json-schema');
const idForDataStructure = (reference) => {
return `definitions/${parseReference(reference)}`;
};
/*
* Data Structure Generator
* Generates a dataStructure element from a Swagger Schema.
*
* >>> const generator = new DataStructureGenerator(minimNamespace);
* >>> const dataStructure = generator.generateDataStructure({type: 'string'});
*/
class DataStructureGenerator {
constructor(minim, root) {
this.minim = minim;
this.root = root;
}
// Generates a data structure element representing the given schema
generateDataStructure(schema) {
const element = this.generateElement(schema);
if (!element) {
return null;
}
const { DataStructure } = this.minim.elements;
const dataStructure = new DataStructure(element);
return dataStructure;
}
// Generates a member element for a property in a schema
generateMember(name, property) {
const {
String: StringElement,
Member: MemberElement,
} = this.minim.elements;
const member = new MemberElement();
member.key = new StringElement(name);
member.value = this.generateElement(property);
return member;
}
// Generates an enum element for the given enum schema
generateEnum(schema) {
const { Enum: EnumElement } = this.minim.elements;
const element = new EnumElement();
element.enumerations = schema.enum;
// eslint-disable-next-line no-restricted-syntax
for (const enumeration of element.enumerations) {
enumeration.attributes.set('typeAttributes', ['fixed']);
}
return element;
}
// Generates an object element from the given object schema
generateObject(schema) {
const {
Object: ObjectElement,
} = this.minim.elements;
const element = new ObjectElement();
let properties = schema.properties || {};
let required = schema.required || [];
if (schema.allOf && Array.isArray(schema.allOf)) {
// Merge all of the object allOf into properties and required
const allOf = schema.allOf.filter(subschema => subschema.type === 'object');
const allProperties = allOf
.filter(subschema => subschema.properties)
.map(subschema => subschema.properties);
properties = Object.assign(properties, ...allProperties);
required = allOf
.filter(subschema => subschema.required)
.map(subschema => subschema.required)
.reduce((accumulator, property) => accumulator.concat(property), required);
const refs = schema.allOf
.filter(subschema => subschema.$ref)
.map(subschema => idForDataStructure(subschema.$ref));
if (refs.length === 1) {
// allOf contains ref, let's treat it as our base
[element.element] = refs;
} else if (refs.length > 1) {
const { Ref: RefElement } = this.minim.elements;
const refElements = refs.map(ref => new RefElement(ref));
element.content.push(...refElements);
}
}
element.content.push(..._.map(properties, (subschema, property) => {
const member = this.generateMember(property, subschema);
const isRequired = required.includes(property);
member.attributes.set('typeAttributes', [
isRequired ? 'required' : 'optional',
]);
return member;
}));
// Create member elements for required keys which are not defined in properties
const missingRequiredProperties = required.filter(name => properties[name] === undefined);
const requiredMembers = missingRequiredProperties.map((name) => {
let member;
if (schema.additionalProperties !== undefined && typeof schema.additionalProperties !== 'boolean') {
member = this.generateMember(name, schema.additionalProperties);
} else {
member = new this.minim.elements.Member(name);
}
member.attributes.set('typeAttributes', ['required']);
return member;
});
element.content.push(...requiredMembers);
if (schema.additionalProperties !== undefined && schema.additionalProperties !== true) {
const typeAttributes = element.attributes.get('typeAttributes') || new this.minim.elements.Array([]);
typeAttributes.push('fixedType');
element.attributes.set('typeAttributes', typeAttributes);
if (schema.additionalProperties !== false) {
let key;
if (schema.example && typeof schema.example === 'object') {
key = Object.keys(schema.example).find(key => element.getMember(key) === undefined);
}
const member = this.generateMember(key || '', schema.additionalProperties);
member.attributes.set('variable', true);
element.content.push(member);
}
}
return element;
}
// Generates an array element from the given array schema
generateArray(schema) {
const { Array: ArrayElement } = this.minim.elements;
const element = new ArrayElement();
if (schema.items) {
element.attributes.set('typeAttributes', ['fixedType']);
if (_.isArray(schema.items)) {
schema.items.forEach((item) => {
const itemElement = this.generateElement(item);
if (itemElement) {
element.push(itemElement);
}
});
} else {
const itemElement = this.generateElement(schema.items);
if (itemElement) {
element.push(itemElement);
}
}
}
return element;
}
// Generates an array of descriptions for each validation rule in the given schema.
generateValidationDescriptions(schema) {
const validations = {
// String
pattern: value => `Matches regex pattern: \`${value}\``,
maxLength: value => `Length of string must be less than, or equal to ${value}`,
minLength: value => `Length of string must be greater than, or equal to ${value}`,
// Number
multipleOf: value => `Number must be a multiple of ${value}`,
maximum: value => `Number must be less than, or equal to ${value}`,
minimum: value => `Number must be more than, or equal to ${value}`,
exclusiveMaximum: value => `Number must be less than ${value}`,
exclusiveMinimum: value => `Number must be more than ${value}`,
// Object
minProperties: value => `Object must have more than, or equal to ${value} properties`,
maxProperties: value => `Object must have less than, or equal to ${value} properties`,
// Array
maxItems: value => `Array length must be less than, or equal to ${value}`,
minItems: value => `Array length must be more than, or equal to ${value}`,
uniqueItems: () => 'Array contents must be unique',
// Other
format: value => `Value must be of format '${value}'`,
};
return _
.chain(validations)
.map((value, key) => {
if (schema[key]) {
return value(schema[key]);
}
return null;
})
.compact()
.value();
}
/**
* Retrieve the type from the schema
* In the case where there is no provided type, the allOf types are matched.
* @param {object} schema
* @returns {string} type
*/
typeForSchema(schema) {
if (schema.$ref) {
// Peek into the reference, if we're calling typeForSchema from the
// allOf case below then we will need to know the destination type
const ref = lookupReference(schema.$ref, this.root);
return this.typeForSchema(ref.referenced);
}
if (schema.type === undefined) {
if (schema.allOf && schema.allOf.length > 0) {
// Try to infer type from allOf values
const allTypes = schema.allOf.map(this.typeForSchema, this);
const uniqueTypes = _.uniq(allTypes);
if (uniqueTypes.length === 1) {
return uniqueTypes[0];
}
}
}
return schema.type;
}
// Generates an element representing the given schema
generateElement(schema) {
const {
String: StringElement,
Number: NumberElement,
Boolean: BooleanElement,
Null: NullElement,
Enum: EnumElement,
// Ref: RefElement,
} = this.minim.elements;
const typeGeneratorMap = {
boolean: BooleanElement,
string: StringElement,
number: NumberElement,
integer: NumberElement,
null: NullElement,
file: StringElement,
};
if (schema.allOf && schema.allOf.length === 1 && schema.definitions
&& Object.keys(schema).length === 2) {
// Since we can't have $ref at root with definitions.
// `allOf` with a single item is used as a work around for this type of schema
// We can safely ignore the allOf and unwrap it as normal schema in this case
return this.generateElement(schema.allOf[0]);
}
const type = this.typeForSchema(schema);
let element;
if (schema.$ref) {
// element = new RefElement(idForDataStructure(schema.$ref));
element = new this.minim.elements.Element();
try {
element.element = idForDataStructure(schema.$ref);
} catch (error) {
// Cannot find ID for reference, let's attempt to dereference the value
// and provide element dereferenced.
// This may be because we cannot express a reference in API Elements,
// for example a reference to `#/definitions/User/properties/name`
const ref = lookupReference(schema.$ref, this.root);
return this.generateElement(ref.referenced);
}
return element;
} if (schema.enum) {
element = this.generateEnum(schema);
} else if (type === 'array') {
element = this.generateArray(schema);
} else if (type === 'object') {
element = this.generateObject(schema);
} else if (type && typeGeneratorMap[type]) {
element = new typeGeneratorMap[type]();
} else if (type) {
throw new Error(`Unhandled schema type '${type}'`);
} else {
element = new this.minim.elements.Enum();
element.enumerations = [
new this.minim.elements.String(),
new this.minim.elements.Number(),
new this.minim.elements.Boolean(),
this.generateArray(schema),
this.generateObject(schema),
];
}
if (element) {
if (schema.title) {
element.title = new StringElement(schema.title);
}
if (schema.description) {
element.description = new StringElement(schema.description);
}
if (schema['x-nullable']) {
element.attributes.set('typeAttributes', ['nullable']);
}
let def = schema.default;
if (def !== undefined && !_.isArray(def) && !_.isObject(def)) {
// TODO Support defaults for arrays and objects
if (schema.enum) {
def = new EnumElement(def);
def.content.attributes.set('typeAttributes', ['fixed']);
}
element.attributes.set('default', def);
}
let samples = [];
if (schema.example) {
samples = [dereference(schema.example, this.root)];
}
if (samples.length > 0) {
if (schema.enum) {
samples = samples.map((item) => {
const enumeration = new EnumElement(item);
enumeration.content.attributes.set('typeAttributes', ['fixed']);
return enumeration;
});
}
const hasSample = samples.length === 1 && samples[0];
const emptyContent = !element.content || element.content.length === 0;
if (hasSample && emptyContent) {
// Convert the sample value to an element as a cheap and easy way to
// check the type matches our element. It will also refract
// object/array items that are the sample value as members/elements
// for us so we can grab its content
const example = this.minim.toElement(samples[0]);
if (element.element === example.element) {
element.content = example.content;
} else {
element.attributes.set('samples', samples);
}
} else {
element.attributes.set('samples', samples);
}
}
const validationDescriptions = this.generateValidationDescriptions(schema);
if (validationDescriptions.length > 0) {
const description = validationDescriptions.map(value => `- ${value}`);
if (element.description && element.description.toValue()) {
description.splice(0, 0, `${element.description.toValue()}\n`);
}
element.description = new StringElement(description.join('\n'));
}
}
return element;
}
}
module.exports = { idForDataStructure, DataStructureGenerator };