@@ -30,6 +30,7 @@ function convertSchema(schema, path, parent, parentPath) {
30
30
schema = stripIllegalKeywords ( schema ) ;
31
31
schema = convertTypes ( schema ) ;
32
32
schema = convertDependencies ( schema ) ;
33
+ schema = rewriteIfThenElse ( schema ) ;
33
34
schema = rewriteExclusiveMinMax ( schema ) ;
34
35
35
36
if ( typeof schema [ 'patternProperties' ] === 'object' ) {
@@ -145,6 +146,28 @@ function convertPatternProperties(schema) {
145
146
return schema ;
146
147
}
147
148
149
+ function rewriteIfThenElse ( schema ) {
150
+ /* @handrews https://github.com/OAI/OpenAPI-Specification/pull/1766#issuecomment-442652805
151
+ if and the *Of keywords
152
+
153
+ There is a really easy solution for implementations, which is that
154
+
155
+ if: X, then: Y, else: Z
156
+
157
+ is equivalent to
158
+
159
+ oneOf: [allOf: [X, Y], allOf: [not: X, Z]]
160
+ */
161
+ if ( schema . if && schema . then ) {
162
+ schema . oneOf = [ { allOf : [ schema . if , schema . then ] } ,
163
+ { allOf : [ { not : schema . if } , schema . else ] } ] ;
164
+ delete schema . if ;
165
+ delete schema . then ;
166
+ delete schema . else ;
167
+ }
168
+ return schema ;
169
+ }
170
+
148
171
function rewriteExclusiveMinMax ( schema ) {
149
172
if ( typeof schema . exclusiveMaximum === 'number' ) {
150
173
schema . maximum = schema . exclusiveMaximum ;
@@ -158,3 +181,4 @@ function rewriteExclusiveMinMax(schema) {
158
181
}
159
182
160
183
module . exports = convert ;
184
+
0 commit comments