@@ -90,7 +90,7 @@ function build (schema, options) {
90
90
91
91
var dependencies = [ ]
92
92
var dependenciesName = [ ]
93
- if ( hasAnyOf ( schema ) ) {
93
+ if ( hasAnyOf ( schema ) || hasArrayOfTypes ( schema ) ) {
94
94
dependencies . push ( new Ajv ( ) )
95
95
dependenciesName . push ( 'ajv' )
96
96
}
@@ -113,6 +113,40 @@ function hasAnyOf (schema) {
113
113
return false
114
114
}
115
115
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
+
116
150
function $asNull ( ) {
117
151
return 'null'
118
152
}
@@ -493,32 +527,7 @@ function buildArray (schema, code, name, externalSchema, fullSchema) {
493
527
result = schema . items . reduce ( ( res , item , i ) => {
494
528
var accessor = '[i]'
495
529
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 ) } `
522
531
return {
523
532
code : `${ res . code }
524
533
${ i > 0 ? 'else' : '' } if (${ condition } ) {
@@ -561,6 +570,43 @@ function buildArray (schema, code, name, externalSchema, fullSchema) {
561
570
return code
562
571
}
563
572
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
+
564
610
function nested ( laterCode , name , key , schema , externalSchema , fullSchema , subKey ) {
565
611
var code = ''
566
612
var funcName
@@ -622,7 +668,19 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
622
668
} else throw new Error ( `${ schema } unsupported` )
623
669
break
624
670
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
+ }
626
684
}
627
685
628
686
return {
0 commit comments