@@ -127,7 +127,7 @@ function build (schema, options) {
127
127
128
128
var dependencies = [ ]
129
129
var dependenciesName = [ ]
130
- if ( hasAnyOf ( schema ) || hasSchemaSomeIf ) {
130
+ if ( hasOf ( schema ) || hasSchemaSomeIf ) {
131
131
dependencies . push ( new Ajv ( options . ajv ) )
132
132
dependenciesName . push ( 'ajv' )
133
133
}
@@ -189,15 +189,15 @@ function inferTypeByKeyword (schema) {
189
189
return schema . type
190
190
}
191
191
192
- function hasAnyOf ( schema ) {
192
+ function hasOf ( schema ) {
193
193
if ( ! schema ) { return false }
194
- if ( 'anyOf' in schema ) { return true }
194
+ if ( 'anyOf' in schema || 'oneOf' in schema ) { return true }
195
195
196
196
var objectKeys = Object . keys ( schema )
197
197
for ( var i = 0 ; i < objectKeys . length ; i ++ ) {
198
198
var value = schema [ objectKeys [ i ] ]
199
199
if ( typeof value === 'object' ) {
200
- if ( hasAnyOf ( value ) ) { return true }
200
+ if ( hasOf ( value ) ) { return true }
201
201
}
202
202
}
203
203
@@ -903,12 +903,12 @@ function buildArrayTypeCondition (type, accessor) {
903
903
return condition
904
904
}
905
905
906
- function dereferenceAnyOfRefs ( schema , externalSchema , fullSchema ) {
907
- schema . anyOf . forEach ( ( s , index ) => {
906
+ function dereferenceOfRefs ( schema , externalSchema , fullSchema , type ) {
907
+ schema [ type ] . forEach ( ( s , index ) => {
908
908
// follow the refs
909
909
while ( s . $ref ) {
910
- schema . anyOf [ index ] = refFinder ( s . $ref , fullSchema , externalSchema )
911
- s = schema . anyOf [ index ]
910
+ schema [ type ] [ index ] = refFinder ( s . $ref , fullSchema , externalSchema )
911
+ s = schema [ type ] [ index ]
912
912
}
913
913
} )
914
914
}
@@ -968,7 +968,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
968
968
break
969
969
case undefined :
970
970
if ( 'anyOf' in schema ) {
971
- dereferenceAnyOfRefs ( schema , externalSchema , fullSchema )
971
+ dereferenceOfRefs ( schema , externalSchema , fullSchema , 'anyOf' )
972
972
schema . anyOf . forEach ( ( s , index ) => {
973
973
var nestedResult = nested ( laterCode , name , key , s , externalSchema , fullSchema , subKey !== '' ? subKey : 'i' + index )
974
974
code += `
@@ -980,6 +980,19 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
980
980
code += `
981
981
else json+= null
982
982
`
983
+ } else if ( 'oneOf' in schema ) {
984
+ dereferenceOfRefs ( schema , externalSchema , fullSchema , 'oneOf' )
985
+ schema . oneOf . forEach ( ( s , index ) => {
986
+ var nestedResult = nested ( laterCode , name , key , s , externalSchema , fullSchema , subKey !== '' ? subKey : 'i' + index )
987
+ code += `
988
+ ${ index === 0 ? 'if' : 'else if' } (ajv.validate(${ require ( 'util' ) . inspect ( s , { depth : null } ) } , obj${ accessor } ))
989
+ ${ nestedResult . code }
990
+ `
991
+ laterCode = nestedResult . laterCode
992
+ } )
993
+ code += `
994
+ else json+= null
995
+ `
983
996
} else if ( isEmpty ( schema ) ) {
984
997
code += `
985
998
json += JSON.stringify(obj${ accessor } )
0 commit comments