1
- const structs = [ 'allOf' , 'anyOf' , 'oneOf' , 'not' , 'items' , 'additionalProperties' ] ;
1
+ const schemaWalker = require ( '@cloudflare/json-schema-walker' ) ;
2
2
3
3
function InvalidTypeError ( message ) {
4
4
this . name = 'InvalidTypeError' ;
@@ -7,61 +7,27 @@ function InvalidTypeError(message) {
7
7
8
8
InvalidTypeError . prototype = new Error ( ) ;
9
9
10
- function convert ( schema , options ) {
11
- options = options || { } ;
12
- options . cloneSchema = ! ( options . cloneSchema === false ) ;
10
+ function convert ( schema , options = { } ) {
11
+ const { cloneSchema = true } = options ;
13
12
14
- if ( options . cloneSchema ) {
13
+ if ( cloneSchema ) {
15
14
schema = JSON . parse ( JSON . stringify ( schema ) ) ;
16
15
}
17
16
18
- schema = removeRootKeywords ( schema ) ;
19
- schema = convertSchema ( schema ) ;
20
-
17
+ const vocab = schemaWalker . getVocabulary ( schema , schemaWalker . vocabularies . DRAFT_04 ) ;
18
+ schemaWalker . schemaWalk ( schema , convertSchema , null , vocab ) ;
21
19
return schema ;
22
20
}
23
21
24
- function removeRootKeywords ( schema ) {
22
+ function stripIllegalKeywords ( schema ) {
25
23
delete schema [ '$schema' ] ;
24
+ delete schema [ '$id' ] ;
26
25
delete schema [ 'id' ] ;
27
26
return schema ;
28
27
}
29
28
30
- function convertSchema ( schema ) {
31
- let i = 0 ;
32
- let j = 0 ;
33
- let struct = null ;
34
-
35
- for ( i ; i < structs . length ; i ++ ) {
36
- struct = structs [ i ] ;
37
-
38
- if ( Array . isArray ( schema [ struct ] ) ) {
39
- for ( j ; j < schema [ struct ] . length ; j ++ ) {
40
- schema [ struct ] [ j ] = convertSchema ( schema [ struct ] [ j ] ) ;
41
- }
42
- } else if ( typeof schema [ struct ] === 'object' ) {
43
- schema [ struct ] = convertSchema ( schema [ struct ] ) ;
44
- }
45
- }
46
-
47
- if ( typeof schema . properties === 'object' ) {
48
- schema . properties = convertProperties ( schema . properties ) ;
49
-
50
- if ( Array . isArray ( schema . required ) ) {
51
- schema . required = cleanRequired ( schema . required , schema . properties ) ;
52
-
53
- if ( schema . required . length === 0 ) {
54
- delete schema . required ;
55
- }
56
- }
57
- if ( Object . keys ( schema . properties ) . length === 0 ) {
58
- delete schema . properties ;
59
- }
60
-
61
- }
62
-
63
- validateType ( schema . type ) ;
64
-
29
+ function convertSchema ( schema , path , parent , parentPath ) {
30
+ schema = stripIllegalKeywords ( schema ) ;
65
31
schema = convertTypes ( schema ) ;
66
32
schema = convertDependencies ( schema ) ;
67
33
@@ -81,19 +47,6 @@ function validateType(type) {
81
47
} ) ;
82
48
}
83
49
84
- function convertProperties ( properties ) {
85
- let key = { } ;
86
- let property = { } ;
87
- let props = { } ;
88
-
89
- for ( key in properties ) {
90
- property = properties [ key ] ;
91
- props [ key ] = convertSchema ( property ) ;
92
- }
93
-
94
- return props ;
95
- }
96
-
97
50
function convertDependencies ( schema ) {
98
51
const deps = schema . dependencies ;
99
52
if ( typeof deps !== 'object' ) {
@@ -140,38 +93,40 @@ function convertDependencies(schema) {
140
93
}
141
94
142
95
function convertTypes ( schema ) {
143
- var newType ;
144
-
145
96
if ( schema . type === undefined ) {
146
97
return schema ;
147
98
}
148
99
149
- // type needs to be a string, not an array
150
- if ( schema . type instanceof Array && schema . type . includes ( 'null' ) ) {
151
- var numTypes = schema . type . length ;
100
+ validateType ( schema . type ) ;
152
101
153
- schema . nullable = true ;
102
+ if ( Array . isArray ( schema . type ) ) {
154
103
155
- // if it was just type: ['null'] for some reason
156
- switch ( numTypes ) {
157
- case 1 :
158
- // Didn't know what else to do
159
- newType = 'string' ;
104
+ if ( schema . type . length > 2 || ! schema . type . includes ( 'null' ) ) {
105
+ throw new Error ( 'Type of ' + schema . type . join ( ',' ) + ' is too confusing for OpenAPI to understand. Found in ' + JSON . stringify ( schema ) ) ;
106
+ }
107
+
108
+ switch ( schema . type . length ) {
109
+ case 0 :
110
+ delete schema . type ;
160
111
break ;
161
112
162
- case 2 :
163
- newType = schema . type . find ( function ( element ) {
164
- return element !== 'null' ;
165
- } ) ;
113
+ case 1 :
114
+ if ( schema . type === 'null' ) {
115
+ schema . nullable = true ;
116
+ }
117
+ else {
118
+ schema . type = schema . type [ 0 ] ;
119
+ }
166
120
break ;
167
121
168
122
default :
169
- throw 'type cannot be an array, and you have ' + numTypes + ' types' ;
123
+ schema . type = schema . type . find ( type => type !== 'null' ) ;
124
+ schema . nullable = true ;
170
125
}
171
126
}
172
-
173
- if ( newType ) {
174
- schema . type = newType ;
127
+ else if ( schema . type === 'null' ) {
128
+ delete schema . type ;
129
+ schema . nullable = true ;
175
130
}
176
131
177
132
return schema ;
@@ -185,19 +140,4 @@ function convertPatternProperties(schema) {
185
140
return schema ;
186
141
}
187
142
188
- function cleanRequired ( required , properties ) {
189
- var i = 0 ;
190
-
191
- required = required || [ ] ;
192
- properties = properties || { } ;
193
-
194
- for ( i ; i < required . length ; i ++ ) {
195
- if ( properties [ required [ i ] ] === undefined ) {
196
- required . splice ( i , 1 ) ;
197
- }
198
- }
199
-
200
- return required ;
201
- }
202
-
203
143
module . exports = convert ;
0 commit comments