@@ -102,8 +102,6 @@ function build (schema, options) {
102102 schema . type = inferTypeByKeyword ( schema )
103103 }
104104
105- var hasSchemaSomeIf = hasIf ( schema )
106-
107105 var main
108106
109107 switch ( schema . type ) {
@@ -141,7 +139,7 @@ function build (schema, options) {
141139
142140 var dependencies = [ ]
143141 var dependenciesName = [ ]
144- if ( hasOf ( schema ) || hasSchemaSomeIf ) {
142+ if ( dependsOnAjv ( schema ) ) {
145143 dependencies . push ( new Ajv ( options . ajv ) )
146144 dependenciesName . push ( 'ajv' )
147145 }
@@ -212,24 +210,9 @@ function inferTypeByKeyword (schema) {
212210 return schema . type
213211}
214212
215- function hasOf ( schema ) {
216- if ( ! schema ) { return false }
217- if ( 'anyOf' in schema || 'oneOf' in schema ) { return true }
218-
219- var objectKeys = Object . keys ( schema )
220- for ( var i = 0 ; i < objectKeys . length ; i ++ ) {
221- var value = schema [ objectKeys [ i ] ]
222- if ( typeof value === 'object' ) {
223- if ( hasOf ( value ) ) { return true }
224- }
225- }
226-
227- return false
228- }
229-
230- function hasIf ( schema ) {
213+ function dependsOnAjv ( schema ) {
231214 const str = JSON . stringify ( schema )
232- return / " i f " : { / . test ( str ) && / " t h e n " : { / . test ( str )
215+ return ( / " i f " : { . * " t h e n " : { | " ( a n y O f | o n e O f ) " : \[ | " c o n s t " : / . test ( str ) )
233216}
234217
235218const stringSerializerMap = {
@@ -1187,6 +1170,13 @@ function nested (laterCode, name, key, location, subKey, isArray) {
11871170 code += `
11881171 json += JSON.stringify(obj${ accessor } )
11891172 `
1173+ } else if ( 'const' in schema ) {
1174+ code += `
1175+ if(ajv.validate(${ require ( 'util' ) . inspect ( schema , { depth : null } ) } , obj${ accessor } ))
1176+ json += '${ JSON . stringify ( schema . const ) } '
1177+ else
1178+ throw new Error(\`Item $\{JSON.stringify(obj${ accessor } )} does not match schema definition.\`)
1179+ `
11901180 } else {
11911181 throw new Error ( `${ schema . type } unsupported` )
11921182 }
0 commit comments