@@ -90,7 +90,7 @@ function build (schema, options) {
9090
9191 var dependencies = [ ]
9292 var dependenciesName = [ ]
93- if ( hasAnyOf ( schema ) ) {
93+ if ( hasAnyOf ( schema ) || hasArrayOfTypes ( schema ) ) {
9494 dependencies . push ( new Ajv ( ) )
9595 dependenciesName . push ( 'ajv' )
9696 }
@@ -113,6 +113,40 @@ function hasAnyOf (schema) {
113113 return false
114114}
115115
116+ function hasArrayOfTypes ( schema ) {
117+ if ( Array . isArray ( schema . type ) ) { return true }
118+ var i
119+
120+ if ( schema . type === 'object' ) {
121+ if ( schema . properties ) {
122+ var propertyKeys = Object . keys ( schema . properties )
123+ for ( i = 0 ; i < propertyKeys . length ; i ++ ) {
124+ if ( hasArrayOfTypes ( schema . properties [ propertyKeys [ i ] ] ) ) {
125+ return true
126+ }
127+ }
128+ }
129+ } else if ( schema . type === 'array' ) {
130+ if ( Array . isArray ( schema . items ) ) {
131+ for ( i = 0 ; i < schema . items . length ; i ++ ) {
132+ if ( hasArrayOfTypes ( schema . items [ i ] ) ) {
133+ return true
134+ }
135+ }
136+ } else if ( schema . items ) {
137+ return hasArrayOfTypes ( schema . items )
138+ }
139+ } else if ( Array . isArray ( schema . anyOf ) ) {
140+ for ( i = 0 ; i < schema . anyOf . length ; i ++ ) {
141+ if ( hasArrayOfTypes ( schema . anyOf [ i ] ) ) {
142+ return true
143+ }
144+ }
145+ }
146+
147+ return false
148+ }
149+
116150function $asNull ( ) {
117151 return 'null'
118152}
@@ -493,32 +527,7 @@ function buildArray (schema, code, name, externalSchema, fullSchema) {
493527 result = schema . items . reduce ( ( res , item , i ) => {
494528 var accessor = '[i]'
495529 const tmpRes = nested ( laterCode , name , accessor , item , externalSchema , fullSchema , i )
496- var condition = `i === ${ i } && `
497- switch ( item . type ) {
498- case 'null' :
499- condition += `obj${ accessor } === null`
500- break
501- case 'string' :
502- condition += `typeof obj${ accessor } === 'string'`
503- break
504- case 'integer' :
505- condition += `Number.isInteger(obj${ accessor } )`
506- break
507- case 'number' :
508- condition += `Number.isFinite(obj${ accessor } )`
509- break
510- case 'boolean' :
511- condition += `typeof obj${ accessor } === 'boolean'`
512- break
513- case 'object' :
514- condition += `obj${ accessor } && typeof obj${ accessor } === 'object' && obj${ accessor } .constructor === Object`
515- break
516- case 'array' :
517- condition += `Array.isArray(obj${ accessor } )`
518- break
519- default :
520- throw new Error ( `${ item . type } unsupported` )
521- }
530+ var condition = `i === ${ i } && ${ buildArrayTypeCondition ( item . type , accessor ) } `
522531 return {
523532 code : `${ res . code }
524533 ${ i > 0 ? 'else' : '' } if (${ condition } ) {
@@ -561,6 +570,43 @@ function buildArray (schema, code, name, externalSchema, fullSchema) {
561570 return code
562571}
563572
573+ function buildArrayTypeCondition ( type , accessor ) {
574+ var condition
575+ switch ( type ) {
576+ case 'null' :
577+ condition = `obj${ accessor } === null`
578+ break
579+ case 'string' :
580+ condition = `typeof obj${ accessor } === 'string'`
581+ break
582+ case 'integer' :
583+ condition = `Number.isInteger(obj${ accessor } )`
584+ break
585+ case 'number' :
586+ condition = `Number.isFinite(obj${ accessor } )`
587+ break
588+ case 'boolean' :
589+ condition = `typeof obj${ accessor } === 'boolean'`
590+ break
591+ case 'object' :
592+ condition = `obj${ accessor } && typeof obj${ accessor } === 'object' && obj${ accessor } .constructor === Object`
593+ break
594+ case 'array' :
595+ condition = `Array.isArray(obj${ accessor } )`
596+ break
597+ default :
598+ if ( Array . isArray ( type ) ) {
599+ var conditions = type . map ( ( subType ) => {
600+ return buildArrayTypeCondition ( subType , accessor )
601+ } )
602+ condition = `(${ conditions . join ( ' || ' ) } )`
603+ } else {
604+ throw new Error ( `${ type } unsupported` )
605+ }
606+ }
607+ return condition
608+ }
609+
564610function nested ( laterCode , name , key , schema , externalSchema , fullSchema , subKey ) {
565611 var code = ''
566612 var funcName
@@ -622,7 +668,19 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
622668 } else throw new Error ( `${ schema } unsupported` )
623669 break
624670 default :
625- throw new Error ( `${ type } unsupported` )
671+ if ( Array . isArray ( type ) ) {
672+ type . forEach ( ( type , index ) => {
673+ var tempSchema = { type : type }
674+ var nestedResult = nested ( laterCode , name , key , tempSchema , externalSchema , fullSchema , subKey )
675+ code += `
676+ ${ index === 0 ? 'if' : 'else if' } (ajv.validate(${ require ( 'util' ) . inspect ( tempSchema , { depth : null } ) } , obj${ accessor } ))
677+ ${ nestedResult . code }
678+ `
679+ laterCode = nestedResult . laterCode
680+ } )
681+ } else {
682+ throw new Error ( `${ type } unsupported` )
683+ }
626684 }
627685
628686 return {
0 commit comments