Skip to content

Commit cf00cad

Browse files
committed
Opening validator for extensions. to provide own validator and/or spec postprocessor
1 parent 9ef5908 commit cf00cad

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/index.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ module.exports = {
5555
'default': validateExamples,
5656
validateFile,
5757
validateExample,
58-
validateExamplesByMap
58+
validateExamplesByMap,
59+
getValidatorFactory
5960
};
6061

6162
// IMPLEMENTATION DETAILS
@@ -98,18 +99,27 @@ module.exports = {
9899
* "unsupported format" errors). If an Array with only one string is
99100
* provided where the formats are separated with `\n`, the entries
100101
* 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
101104
* @returns {ValidationResponse}
102105
*/
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+
} = {}) {
104110
const impl = Determiner.getImplementation(openapiSpec);
105111
openapiSpec = await refParser.dereference(openapiSpec);
106112
openapiSpec = impl.prepare(openapiSpec, { noAdditionalProperties, allPropertiesRequired });
113+
if (typeof specPostprocessor === 'function') {
114+
openapiSpec = specPostprocessor(openapiSpec);
115+
}
107116
let pathsExamples = impl.getJsonPathsToExamples()
108117
.reduce((res, pathToExamples) => {
109118
return res.concat(_extractExamplePaths(openapiSpec, pathToExamples));
110119
}, [])
111120
.map(impl.escapeExampleName);
112-
return _validateExamplesPaths({ impl }, pathsExamples, openapiSpec, { ignoreFormats });
121+
const createValidator = validatorFactory(openapiSpec, { ignoreFormats });
122+
return _validateExamplesPaths({ impl, createValidator }, pathsExamples, openapiSpec);
113123
}
114124

115125
/**
@@ -413,23 +423,19 @@ function _extractExamplePaths(openapiSpec, jsonPathToExamples) {
413423
/**
414424
* Validates examples at the given paths in the OpenAPI-spec.
415425
* @param {Object} impl Spec-dependant validator
426+
* @param {Function} createValidator Validator factory
416427
* @param {Array.<String>} pathsExamples JSON-paths to examples
417428
* @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.
422429
* @returns {ValidationResponse}
423430
* @private
424431
*/
425-
function _validateExamplesPaths({ impl }, pathsExamples, openapiSpec, { ignoreFormats }) {
432+
function _validateExamplesPaths({ impl, createValidator }, pathsExamples, openapiSpec) {
426433
const statistics = _initStatistics(),
427434
validationResult = {
428435
valid: true,
429436
statistics,
430437
errors: []
431-
},
432-
createValidator = _initValidatorFactory(openapiSpec, { ignoreFormats });
438+
};
433439
let validationMap;
434440
try {
435441
// Create mapping between JSON-schemas and examples

src/validator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ module.exports = {
2323
* @param {Object} [options] Options for the validator
2424
* @returns {function(): (ajv | ajv.Ajv)}
2525
*/
26-
function getValidatorFactory(specSchema, options) {
26+
function getValidatorFactory(specSchema, options, { provider = (opt) => new Ajv(opt) }) {
2727
const preparedSpecSchema = _createReferenceSchema(specSchema);
2828
return () => {
29-
const validator = new Ajv(options);
29+
const validator = provider(options);
3030
addFormats(validator);
3131

3232
validator.addSchema(preparedSpecSchema);

0 commit comments

Comments
 (0)