@@ -94,7 +94,7 @@ function build (schema, options) {
94
94
95
95
var dependencies = [ ]
96
96
var dependenciesName = [ ]
97
- if ( hasAnyOf ( schema ) || hasArrayOfTypes ( schema ) || hasShemaSomeIf ) {
97
+ if ( hasAnyOf ( schema ) || hasShemaSomeIf ) {
98
98
dependencies . push ( new Ajv ( ) )
99
99
dependenciesName . push ( 'ajv' )
100
100
}
@@ -117,40 +117,6 @@ function hasAnyOf (schema) {
117
117
return false
118
118
}
119
119
120
- function hasArrayOfTypes ( schema ) {
121
- if ( Array . isArray ( schema . type ) ) { return true }
122
- var i
123
-
124
- if ( schema . type === 'object' ) {
125
- if ( schema . properties ) {
126
- var propertyKeys = Object . keys ( schema . properties )
127
- for ( i = 0 ; i < propertyKeys . length ; i ++ ) {
128
- if ( hasArrayOfTypes ( schema . properties [ propertyKeys [ i ] ] ) ) {
129
- return true
130
- }
131
- }
132
- }
133
- } else if ( schema . type === 'array' ) {
134
- if ( Array . isArray ( schema . items ) ) {
135
- for ( i = 0 ; i < schema . items . length ; i ++ ) {
136
- if ( hasArrayOfTypes ( schema . items [ i ] ) ) {
137
- return true
138
- }
139
- }
140
- } else if ( schema . items ) {
141
- return hasArrayOfTypes ( schema . items )
142
- }
143
- } else if ( Array . isArray ( schema . anyOf ) ) {
144
- for ( i = 0 ; i < schema . anyOf . length ; i ++ ) {
145
- if ( hasArrayOfTypes ( schema . anyOf [ i ] ) ) {
146
- return true
147
- }
148
- }
149
- }
150
-
151
- return false
152
- }
153
-
154
120
function hasIf ( schema ) {
155
121
const str = JSON . stringify ( schema )
156
122
return / " i f " : { / . test ( str ) && / " t h e n " : { / . test ( str )
@@ -764,17 +730,29 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
764
730
break
765
731
default :
766
732
if ( Array . isArray ( type ) ) {
767
- type . forEach ( ( type , index ) => {
768
- var tempSchema = { type : type }
733
+ const nullIndex = type . indexOf ( 'null' )
734
+ const sortedTypes = nullIndex !== - 1 ? [ type [ nullIndex ] ] . concat ( type . slice ( 0 , nullIndex ) ) . concat ( type . slice ( nullIndex + 1 ) ) : type
735
+ sortedTypes . forEach ( ( type , index ) => {
736
+ var tempSchema = Object . assign ( { } , schema , { type} )
769
737
var nestedResult = nested ( laterCode , name , key , tempSchema , externalSchema , fullSchema , subKey )
770
738
if ( type === 'string' ) {
771
739
code += `
772
- ${ index === 0 ? 'if' : 'else if' } (obj${ accessor } instanceof Date || ajv.validate( ${ require ( 'util' ) . inspect ( tempSchema , { depth : null } ) } , obj${ accessor } ) )
740
+ ${ index === 0 ? 'if' : 'else if' } (typeof obj${ accessor } === " ${ type } " || obj ${ accessor } instanceof Date || obj${ accessor } instanceof RegExp )
773
741
${ nestedResult . code }
774
742
`
743
+ } else if ( type === 'null' ) {
744
+ code += `
745
+ ${ index === 0 ? 'if' : 'else if' } (obj${ accessor } == null)
746
+ ${ nestedResult . code }
747
+ `
748
+ } else if ( type === 'array' ) {
749
+ code += `
750
+ ${ index === 0 ? 'if' : 'else if' } (Array.isArray(obj${ accessor } ))
751
+ ${ nestedResult . code }
752
+ `
775
753
} else {
776
754
code += `
777
- ${ index === 0 ? 'if' : 'else if' } (ajv.validate( ${ require ( 'util' ) . inspect ( tempSchema , { depth : null } ) } , obj ${ accessor } ) )
755
+ ${ index === 0 ? 'if' : 'else if' } (typeof obj ${ accessor } === " ${ type } " )
778
756
${ nestedResult . code }
779
757
`
780
758
}
0 commit comments