@@ -102,8 +102,6 @@ function build (schema, options) {
102
102
schema . type = inferTypeByKeyword ( schema )
103
103
}
104
104
105
- var hasSchemaSomeIf = hasIf ( schema )
106
-
107
105
var main
108
106
109
107
switch ( schema . type ) {
@@ -141,7 +139,7 @@ function build (schema, options) {
141
139
142
140
var dependencies = [ ]
143
141
var dependenciesName = [ ]
144
- if ( hasOf ( schema ) || hasSchemaSomeIf ) {
142
+ if ( dependsOnAjv ( schema ) ) {
145
143
dependencies . push ( new Ajv ( options . ajv ) )
146
144
dependenciesName . push ( 'ajv' )
147
145
}
@@ -212,24 +210,9 @@ function inferTypeByKeyword (schema) {
212
210
return schema . type
213
211
}
214
212
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 ) {
231
214
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 ) )
233
216
}
234
217
235
218
const stringSerializerMap = {
@@ -1187,6 +1170,13 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1187
1170
code += `
1188
1171
json += JSON.stringify(obj${ accessor } )
1189
1172
`
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
+ `
1190
1180
} else {
1191
1181
throw new Error ( `${ schema . type } unsupported` )
1192
1182
}
0 commit comments