@@ -30,6 +30,9 @@ export default class JSONSchemaView {
30
30
this . options = options ;
31
31
this . isCollapsed = open <= 0 ;
32
32
33
+ // Guard against empty schemas
34
+ if ( ! this . schema ) return '' ;
35
+
33
36
// if schema is an empty object which means any JOSN
34
37
this . isAny = typeof schema === 'object' &&
35
38
! Array . isArray ( schema ) &&
@@ -39,6 +42,11 @@ export default class JSONSchemaView {
39
42
// Determine if a schema is an array
40
43
this . isArray = ! this . isAny && this . schema && this . schema . type === 'array' ;
41
44
45
+ // Determine if a schema is a collection of types (an Array with at least one object in it)
46
+ this . isCollectionOfTypes = this . schema &&
47
+ Array . isArray ( this . schema . type ) &&
48
+ this . schema . type . reduce ( ( item ) => typeof item === 'object' ) ;
49
+
42
50
this . isObject = this . schema &&
43
51
( this . schema . type === 'object' ||
44
52
this . schema . properties ||
@@ -47,7 +55,7 @@ export default class JSONSchemaView {
47
55
this . schema . allOf ) ;
48
56
49
57
// Determine if a schema is a primitive
50
- this . isPrimitive = ! this . isAny && ! this . isArray && ! this . isObject ;
58
+ this . isPrimitive = ! this . isAny && ! this . isArray && ! this . isObject && ! this . isCollectionOfTypes ;
51
59
52
60
//
53
61
this . showToggle = this . schema . description ||
@@ -72,6 +80,13 @@ export default class JSONSchemaView {
72
80
}
73
81
} ) ;
74
82
}
83
+
84
+ // Create a list of types as a string for Collections of types
85
+ if ( this . isCollectionOfTypes ) {
86
+ this . typeList = this . schema . type . reduce ( ( prev , curr ) => {
87
+ return prev . type + ', ' + curr . type ;
88
+ } ) ;
89
+ }
75
90
}
76
91
77
92
/*
@@ -190,7 +205,7 @@ export default class JSONSchemaView {
190
205
` }
191
206
192
207
<!-- Object -->
193
- ${ _if ( ! this . isPrimitive && ! this . isArray && ! this . isAny ) `
208
+ ${ _if ( ! this . isPrimitive && ! this . isArray && ! this . isAny && ! this . isCollectionOfTypes ) `
194
209
<div class="object">
195
210
<a class="title"><span
196
211
class="toggle-handle"></span>${ this . schema . title || '' } <span
@@ -218,6 +233,20 @@ export default class JSONSchemaView {
218
233
` }
219
234
</div>
220
235
` }
236
+
237
+ <!-- Type Array -->
238
+ ${ _if ( this . isCollectionOfTypes ) `
239
+ <div class="collectionOfTypes">
240
+ <a class="title"><span class="toggle-handle"></span>${ this . schema . title || '' } </a>
241
+ <span class="type">${ this . typeList } </span>
242
+ <div class="inner">
243
+ ${ _if ( this . schema . description && ! this . isCollapsed ) `
244
+ <div class="description">${ this . schema . description } </div>
245
+ ` }
246
+ <!-- children go here -->
247
+ </div>
248
+ </div>
249
+ ` }
221
250
` . replace ( / \s * \n / g, '\n' ) . replace ( / ( \< \! \- \- ) .+ / g, '' ) . trim ( ) ;
222
251
}
223
252
@@ -312,6 +341,15 @@ export default class JSONSchemaView {
312
341
inner . appendChild ( view . render ( ) ) ;
313
342
}
314
343
344
+ if ( this . isCollectionOfTypes ) {
345
+ const openLevel = this . open - 1 ;
346
+ this . schema . type . forEach ( ( type ) => {
347
+ const view = new JSONSchemaView ( type , openLevel ) ;
348
+ inner . appendChild ( view . render ( ) ) ;
349
+ } ) ;
350
+
351
+ }
352
+
315
353
if ( typeof this . schema . properties === 'object' ) {
316
354
Object . keys ( this . schema . properties ) . forEach ( propertyName => {
317
355
const property = this . schema . properties [ propertyName ] ;
0 commit comments