Skip to content

Commit b501d33

Browse files
fox1tEomm
andauthored
Adds support for oneOf schemas (#212)
* Adds support for oneOf schemas * Update test/oneof.test.js Co-Authored-By: Manuel Spigolon <[email protected]> * Update test/oneof.test.js Co-Authored-By: Manuel Spigolon <[email protected]> Co-authored-by: Manuel Spigolon <[email protected]>
1 parent 1786bc0 commit b501d33

File tree

2 files changed

+421
-9
lines changed

2 files changed

+421
-9
lines changed

index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function build (schema, options) {
127127

128128
var dependencies = []
129129
var dependenciesName = []
130-
if (hasAnyOf(schema) || hasSchemaSomeIf) {
130+
if (hasOf(schema) || hasSchemaSomeIf) {
131131
dependencies.push(new Ajv(options.ajv))
132132
dependenciesName.push('ajv')
133133
}
@@ -189,15 +189,15 @@ function inferTypeByKeyword (schema) {
189189
return schema.type
190190
}
191191

192-
function hasAnyOf (schema) {
192+
function hasOf (schema) {
193193
if (!schema) { return false }
194-
if ('anyOf' in schema) { return true }
194+
if ('anyOf' in schema || 'oneOf' in schema) { return true }
195195

196196
var objectKeys = Object.keys(schema)
197197
for (var i = 0; i < objectKeys.length; i++) {
198198
var value = schema[objectKeys[i]]
199199
if (typeof value === 'object') {
200-
if (hasAnyOf(value)) { return true }
200+
if (hasOf(value)) { return true }
201201
}
202202
}
203203

@@ -903,12 +903,12 @@ function buildArrayTypeCondition (type, accessor) {
903903
return condition
904904
}
905905

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) => {
908908
// follow the refs
909909
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]
912912
}
913913
})
914914
}
@@ -968,7 +968,7 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
968968
break
969969
case undefined:
970970
if ('anyOf' in schema) {
971-
dereferenceAnyOfRefs(schema, externalSchema, fullSchema)
971+
dereferenceOfRefs(schema, externalSchema, fullSchema, 'anyOf')
972972
schema.anyOf.forEach((s, index) => {
973973
var nestedResult = nested(laterCode, name, key, s, externalSchema, fullSchema, subKey !== '' ? subKey : 'i' + index)
974974
code += `
@@ -980,6 +980,19 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
980980
code += `
981981
else json+= null
982982
`
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+
`
983996
} else if (isEmpty(schema)) {
984997
code += `
985998
json += JSON.stringify(obj${accessor})

0 commit comments

Comments
 (0)