@@ -55,7 +55,8 @@ module.exports = {
55
55
'default' : validateExamples ,
56
56
validateFile,
57
57
validateExample,
58
- validateExamplesByMap
58
+ validateExamplesByMap,
59
+ getValidatorFactory
59
60
} ;
60
61
61
62
// IMPLEMENTATION DETAILS
@@ -98,18 +99,27 @@ module.exports = {
98
99
* "unsupported format" errors). If an Array with only one string is
99
100
* provided where the formats are separated with `\n`, the entries
100
101
* will be expanded to a new array containing all entries.
102
+ * @param {Function } [specPostprocessor] Provides implementation of spec postprocessor
103
+ * @param {Function } [validatorFactory] Validator factory provider
101
104
* @returns {ValidationResponse }
102
105
*/
103
- async function validateExamples ( openapiSpec , { noAdditionalProperties, ignoreFormats, allPropertiesRequired } = { } ) {
106
+ async function validateExamples ( openapiSpec , { noAdditionalProperties, ignoreFormats, allPropertiesRequired,
107
+ specPostprocessor = ( spec ) => spec ,
108
+ validatorFactory = ( spec , { ignoreFormats } ) => _initValidatorFactory ( spec , { ignoreFormats } )
109
+ } = { } ) {
104
110
const impl = Determiner . getImplementation ( openapiSpec ) ;
105
111
openapiSpec = await refParser . dereference ( openapiSpec ) ;
106
112
openapiSpec = impl . prepare ( openapiSpec , { noAdditionalProperties, allPropertiesRequired } ) ;
113
+ if ( typeof specPostprocessor === 'function' ) {
114
+ openapiSpec = specPostprocessor ( openapiSpec ) ;
115
+ }
107
116
let pathsExamples = impl . getJsonPathsToExamples ( )
108
117
. reduce ( ( res , pathToExamples ) => {
109
118
return res . concat ( _extractExamplePaths ( openapiSpec , pathToExamples ) ) ;
110
119
} , [ ] )
111
120
. map ( impl . escapeExampleName ) ;
112
- return _validateExamplesPaths ( { impl } , pathsExamples , openapiSpec , { ignoreFormats } ) ;
121
+ const createValidator = validatorFactory ( openapiSpec , { ignoreFormats } ) ;
122
+ return _validateExamplesPaths ( { impl, createValidator } , pathsExamples , openapiSpec ) ;
113
123
}
114
124
115
125
/**
@@ -413,23 +423,19 @@ function _extractExamplePaths(openapiSpec, jsonPathToExamples) {
413
423
/**
414
424
* Validates examples at the given paths in the OpenAPI-spec.
415
425
* @param {Object } impl Spec-dependant validator
426
+ * @param {Function } createValidator Validator factory
416
427
* @param {Array.<String> } pathsExamples JSON-paths to examples
417
428
* @param {Object } openapiSpec OpenAPI-spec
418
- * @param {Array.<string> } [ignoreFormats] List of datatype formats that shall be ignored (to prevent
419
- * "unsupported format" errors). If an Array with only one string is
420
- * provided where the formats are separated with `\n`, the entries
421
- * will be expanded to a new array containing all entries.
422
429
* @returns {ValidationResponse }
423
430
* @private
424
431
*/
425
- function _validateExamplesPaths ( { impl } , pathsExamples , openapiSpec , { ignoreFormats } ) {
432
+ function _validateExamplesPaths ( { impl, createValidator } , pathsExamples , openapiSpec ) {
426
433
const statistics = _initStatistics ( ) ,
427
434
validationResult = {
428
435
valid : true ,
429
436
statistics,
430
437
errors : [ ]
431
- } ,
432
- createValidator = _initValidatorFactory ( openapiSpec , { ignoreFormats } ) ;
438
+ } ;
433
439
let validationMap ;
434
440
try {
435
441
// Create mapping between JSON-schemas and examples
0 commit comments