@@ -105,20 +105,18 @@ module.exports = {
105
105
*/
106
106
async function validateExamples ( openapiSpec , { noAdditionalProperties, ignoreFormats, allPropertiesRequired,
107
107
specPostprocessor = ( spec ) => spec ,
108
- validatorFactory = ( spec , { ignoreFormats } ) => _initValidatorFactory ( spec , { ignoreFormats } )
108
+ validatorFactory = ( spec , ignoreFormats ) => _initValidatorFactory ( spec , { ignoreFormats } )
109
109
} = { } ) {
110
110
const impl = Determiner . getImplementation ( openapiSpec ) ;
111
111
openapiSpec = await refParser . dereference ( openapiSpec ) ;
112
112
openapiSpec = impl . prepare ( openapiSpec , { noAdditionalProperties, allPropertiesRequired } ) ;
113
- if ( typeof specPostprocessor === 'function' ) {
114
- openapiSpec = specPostprocessor ( openapiSpec ) ;
115
- }
113
+ openapiSpec = _postprocess ( openapiSpec , specPostprocessor ) ;
116
114
let pathsExamples = impl . getJsonPathsToExamples ( )
117
115
. reduce ( ( res , pathToExamples ) => {
118
116
return res . concat ( _extractExamplePaths ( openapiSpec , pathToExamples ) ) ;
119
117
} , [ ] )
120
118
. map ( impl . escapeExampleName ) ;
121
- const createValidator = validatorFactory ( openapiSpec , { ignoreFormats } ) ;
119
+ const createValidator = validatorFactory ( openapiSpec , ignoreFormats ) ;
122
120
return _validateExamplesPaths ( { impl, createValidator } , pathsExamples , openapiSpec ) ;
123
121
}
124
122
@@ -131,16 +129,24 @@ async function validateExamples(openapiSpec, { noAdditionalProperties, ignoreFor
131
129
* "unsupported format" errors). If an Array with only one string is
132
130
* provided where the formats are separated with `\n`, the entries
133
131
* will be expanded to a new array containing all entries.
132
+ * @param {Function } [specPostprocessor] Provides implementation of spec postprocessor
133
+ * @param {Function } [validatorFactory] Validator factory provider
134
134
* @returns {ValidationResponse }
135
135
*/
136
- async function validateFile ( filePath , { noAdditionalProperties, ignoreFormats, allPropertiesRequired } = { } ) {
136
+ async function validateFile ( filePath , { noAdditionalProperties, ignoreFormats, allPropertiesRequired,
137
+ specPostprocessor = ( spec ) => spec ,
138
+ validatorFactory = ( spec , ignoreFormats ) => _initValidatorFactory ( spec , { ignoreFormats } )
139
+ } = { } ) {
137
140
let openapiSpec = null ;
138
141
try {
139
142
openapiSpec = await _parseSpec ( filePath ) ;
140
143
} catch ( err ) {
141
144
return createValidationResponse ( { errors : [ ApplicationError . create ( err ) ] } ) ;
142
145
}
143
- return validateExamples ( openapiSpec , { noAdditionalProperties, ignoreFormats, allPropertiesRequired } ) ;
146
+ return validateExamples ( openapiSpec , {
147
+ noAdditionalProperties, ignoreFormats, allPropertiesRequired,
148
+ specPostprocessor, validatorFactory
149
+ } ) ;
144
150
}
145
151
146
152
/**
@@ -157,11 +163,15 @@ async function validateFile(filePath, { noAdditionalProperties, ignoreFormats, a
157
163
* "unsupported format" errors). If an Array with only one string is
158
164
* provided where the formats are separated with `\n`, the entries
159
165
* will be expanded to a new array containing all entries.
166
+ * @param {Function } [specPostprocessor] Provides implementation of spec postprocessor
167
+ * @param {Function } [validatorFactory] Validator factory provider
160
168
* @returns {ValidationResponse }
161
169
*/
162
170
async function validateExamplesByMap ( filePathSchema , globMapExternalExamples ,
163
- { cwdToMappingFile, noAdditionalProperties, ignoreFormats, allPropertiesRequired } = { }
164
- ) {
171
+ { cwdToMappingFile, noAdditionalProperties, ignoreFormats, allPropertiesRequired,
172
+ specPostprocessor = ( spec ) => spec ,
173
+ validatorFactory = ( spec , ignoreFormats ) => _initValidatorFactory ( spec , { ignoreFormats } )
174
+ } = { } ) {
165
175
let matchingFilePathsMapping = 0 ;
166
176
const filePathsMaps = glob . sync (
167
177
globMapExternalExamples ,
@@ -179,6 +189,7 @@ async function validateExamplesByMap(filePathSchema, globMapExternalExamples,
179
189
openapiSpec = await _parseSpec ( filePathSchema ) ;
180
190
openapiSpec = Determiner . getImplementation ( openapiSpec )
181
191
. prepare ( openapiSpec , { noAdditionalProperties, allPropertiesRequired } ) ;
192
+ openapiSpec = _postprocess ( openapiSpec , specPostprocessor ) ;
182
193
} catch ( err ) {
183
194
responses . push ( createValidationResponse ( { errors : [ ApplicationError . create ( err ) ] } ) ) ;
184
195
continue ;
@@ -189,13 +200,11 @@ async function validateExamplesByMap(filePathSchema, globMapExternalExamples,
189
200
responses . push (
190
201
_validate (
191
202
statistics => {
192
- return _handleExamplesByMapValidation (
193
- openapiSpec , mapExternalExamples , statistics , {
194
- cwdToMappingFile,
195
- dirPathMapExternalExamples : path . dirname ( filePathMapExternalExamples ) ,
196
- ignoreFormats
197
- }
198
- ) . map (
203
+ return _handleExamplesByMapValidation ( openapiSpec , mapExternalExamples , statistics , {
204
+ cwdToMappingFile,
205
+ dirPathMapExternalExamples : path . dirname ( filePathMapExternalExamples ) ,
206
+ ignoreFormats, validatorFactory
207
+ } ) . map (
199
208
( /** @type ApplicationError */ error ) => Object . assign ( error , {
200
209
mapFilePath : path . normalize ( filePathMapExternalExamples )
201
210
} )
@@ -226,12 +235,16 @@ async function validateExamplesByMap(filePathSchema, globMapExternalExamples,
226
235
* "unsupported format" errors). If an Array with only one string is
227
236
* provided where the formats are separated with `\n`, the entries
228
237
* will be expanded to a new array containing all entries.
238
+ * @param {Function } [specPostprocessor] Provides implementation of spec postprocessor
239
+ * @param {Function } [validatorFactory] Validator factory provider
229
240
* @returns {ValidationResponse }
230
241
*/
231
242
async function validateExample ( filePathSchema , pathSchema , filePathExample , {
232
243
noAdditionalProperties,
233
244
ignoreFormats,
234
- allPropertiesRequired
245
+ allPropertiesRequired,
246
+ specPostprocessor = ( spec ) => spec ,
247
+ validatorFactory = ( spec , ignoreFormats ) => _initValidatorFactory ( spec , { ignoreFormats } )
235
248
} = { } ) {
236
249
let example = null ,
237
250
schema = null ,
@@ -241,13 +254,15 @@ async function validateExample(filePathSchema, pathSchema, filePathExample, {
241
254
openapiSpec = await _parseSpec ( filePathSchema ) ;
242
255
openapiSpec = Determiner . getImplementation ( openapiSpec )
243
256
. prepare ( openapiSpec , { noAdditionalProperties, allPropertiesRequired } ) ;
257
+ openapiSpec = _postprocess ( openapiSpec , specPostprocessor ) ;
244
258
schema = _extractSchema ( pathSchema , openapiSpec ) ;
245
259
} catch ( err ) {
246
260
return createValidationResponse ( { errors : [ ApplicationError . create ( err ) ] } ) ;
247
261
}
262
+ const createValidator = validatorFactory ( openapiSpec , ignoreFormats ) ;
248
263
return _validate (
249
264
statistics => _validateExample ( {
250
- createValidator : _initValidatorFactory ( openapiSpec , { ignoreFormats } ) ,
265
+ createValidator,
251
266
schema,
252
267
example,
253
268
statistics,
@@ -322,11 +337,12 @@ function _validate(validationHandler) {
322
337
* "unsupported format" errors). If an Array with only one string is
323
338
* provided where the formats are separated with `\n`, the entries
324
339
* will be expanded to a new array containing all entries.
340
+ * @param {Function } [validatorFactory] Validator factory provider
325
341
* @returns {Array.<ApplicationError> }
326
342
* @private
327
343
*/
328
344
function _handleExamplesByMapValidation ( openapiSpec , mapExternalExamples , statistics ,
329
- { cwdToMappingFile = false , dirPathMapExternalExamples, ignoreFormats }
345
+ { cwdToMappingFile = false , dirPathMapExternalExamples, ignoreFormats, validatorFactory }
330
346
) {
331
347
return flatMap ( Object . entries ( mapExternalExamples ) , ( [ pathSchema , filePathsExample ] ) => {
332
348
let schema = null ;
@@ -336,6 +352,8 @@ function _handleExamplesByMapValidation(openapiSpec, mapExternalExamples, statis
336
352
// If the schema can't be found, don't even attempt to process the examples
337
353
return ApplicationError . create ( err ) ;
338
354
}
355
+ const createValidator = validatorFactory ( openapiSpec , ignoreFormats ) ;
356
+
339
357
return flatMap (
340
358
flatten ( [ filePathsExample ] ) ,
341
359
filePathExample => {
@@ -362,7 +380,7 @@ function _handleExamplesByMapValidation(openapiSpec, mapExternalExamples, statis
362
380
return [ ApplicationError . create ( err ) ] ;
363
381
}
364
382
return flatMap ( examples , example => _validateExample ( {
365
- createValidator : _initValidatorFactory ( openapiSpec , { ignoreFormats } ) ,
383
+ createValidator,
366
384
schema,
367
385
example : example . content ,
368
386
statistics,
@@ -429,7 +447,7 @@ function _extractExamplePaths(openapiSpec, jsonPathToExamples) {
429
447
* @returns {ValidationResponse }
430
448
* @private
431
449
*/
432
- function _validateExamplesPaths ( { impl, createValidator } , pathsExamples , openapiSpec ) {
450
+ function _validateExamplesPaths ( { impl, createValidator } , pathsExamples , openapiSpec ) {
433
451
const statistics = _initStatistics ( ) ,
434
452
validationResult = {
435
453
valid : true ,
@@ -581,18 +599,32 @@ function _validateExample({ createValidator, schema, example, statistics, filePa
581
599
* @private
582
600
*/
583
601
function _initValidatorFactory ( specSchema , { ignoreFormats } ) {
602
+ const formats = ignoreFormats && ignoreFormats . reduce ( ( result , entry ) => {
603
+ result [ entry ] = ( ) => true ;
604
+ return result ;
605
+ } , { } ) ;
584
606
return getValidatorFactory ( specSchema , {
585
607
schemaId : 'auto' ,
586
608
discriminator : true ,
587
609
strict : false ,
588
610
allErrors : true ,
589
- formats : ignoreFormats && ignoreFormats . reduce ( ( result , entry ) => {
590
- result [ entry ] = ( ) => true ;
591
- return result ;
592
- } , { } )
611
+ formats
593
612
} ) ;
594
613
}
595
614
615
+ /***
616
+ * Run spec postprocess if defined.
617
+ * @returns modified OAS spec or the original one in case of errors
618
+ */
619
+ function _postprocess ( oasSpec , specPostprocessor ) {
620
+ let result = oasSpec ;
621
+ if ( typeof specPostprocessor === 'function' ) {
622
+ result = specPostprocessor ( oasSpec ) ;
623
+ if ( ! result ) { throw new Error ( 'Postprocessor has to be specified' ) ; }
624
+ }
625
+ return result ;
626
+ }
627
+
596
628
/**
597
629
* Extracts the schema in the OpenAPI-spec at the given JSON-path.
598
630
* @param {string } pathSchema JSON-path to the schema
0 commit comments