Added support for anyOf and oneOf keywords in validation flow

This commit is contained in:
Vishal Shingala
2021-10-21 00:00:48 +05:30
parent d97ca06c55
commit 65d7e9af23
7 changed files with 447 additions and 9 deletions

View File

@@ -2986,7 +2986,8 @@ module.exports = {
// This is dereferenced schema (converted to JSON schema for validation)
schema = deref.resolveRefs(openApiSchemaObj, parameterSourceOption, components,
schemaCache.schemaResolutionCache, PROCESSING_TYPE.VALIDATION, 'example', 0, {}, options.stackLimit);
schemaCache.schemaResolutionCache, PROCESSING_TYPE.VALIDATION, 'example', 0, {}, options.stackLimit),
compositeSchema = schema.oneOf || schema.anyOf;
if (needJsonMatching) {
try {
@@ -3001,8 +3002,29 @@ module.exports = {
}
}
// For anyOf and oneOf schemas, validate value against each schema and report result with least mismatches
if (compositeSchema) {
// get mismatches of value against each schema
async.map(compositeSchema, (elementSchema, cb) => {
setTimeout(() => {
this.checkValueAgainstSchema(property, jsonPathPrefix, txnParamName, value,
`${schemaPathPrefix}.${schema.oneOf ? 'oneOf' : 'anyOf'}[${_.findIndex(compositeSchema, elementSchema)}]`,
elementSchema, parameterSourceOption, components, options, schemaCache, cb);
}, 0);
}, (err, results) => {
let sortedResults;
if (err) {
return callback(err, []);
}
// return mismatches of schema against which least validation mismatches were found
sortedResults = _.sortBy(results, (res) => { return res.length; });
return callback(null, sortedResults[0]);
});
}
// When processing a reference, schema.type could also be undefined
if (schema && schema.type) {
else if (schema && schema.type) {
if (typeof schemaTypeToJsValidator[schema.type] === 'function') {
let isCorrectType;
@@ -3168,6 +3190,7 @@ module.exports = {
return callback(null, mismatches);
}
// result passed. No AJV mismatch
return callback(null, []);
}
// Schema was not AJV or object
@@ -3186,15 +3209,20 @@ module.exports = {
}
}]);
}
else {
return callback(null, []);
}
}
else {
// unknown schema.type found
// TODO: Decide how to handle. Log?
return callback(null, []);
}
}
// Schema not defined
return callback(null, []);
else {
return callback(null, []);
}
// if (!schemaTypeToJsValidator[schema.type](value)) {
// callback(null, [{
// property,