@@ -94,7 +94,7 @@ function build (schema, options) {
9494
9595 var dependencies = [ ]
9696 var dependenciesName = [ ]
97- if ( hasAnyOf ( schema ) || hasArrayOfTypes ( schema ) || hasShemaSomeIf ) {
97+ if ( hasAnyOf ( schema ) || hasShemaSomeIf ) {
9898 dependencies . push ( new Ajv ( ) )
9999 dependenciesName . push ( 'ajv' )
100100 }
@@ -117,40 +117,6 @@ function hasAnyOf (schema) {
117117 return false
118118}
119119
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-
154120function hasIf ( schema ) {
155121 const str = JSON . stringify ( schema )
156122 return / " i f " : { / . test ( str ) && / " t h e n " : { / . test ( str )
@@ -764,17 +730,29 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
764730 break
765731 default :
766732 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} )
769737 var nestedResult = nested ( laterCode , name , key , tempSchema , externalSchema , fullSchema , subKey )
770738 if ( type === 'string' ) {
771739 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 )
773741 ${ nestedResult . code }
774742 `
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+ `
775753 } else {
776754 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 } " )
778756 ${ nestedResult . code }
779757 `
780758 }
0 commit comments