Skip to content

Adds support for validation mismatches severity. #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/ajValidation/ajvValidationError.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
var _ = require('lodash');
const { formatDataPath } = require('../common/schemaUtilsCommon');
const { formatDataPath } = require('../common/schemaUtilsCommon'),
// Mismatch severities
SEVERITY = {
INFO: 'info',
LOG: 'log',
WARNING: 'warning',
ERROR: 'error'
};

/**
* This function generates reason and reasonCodes (used in mismatch objects) using Ajv Validation Error
Expand All @@ -21,13 +28,15 @@ function ajvValidationError(ajvValidationErrorObj, data) {
mismatchObj = {
reason: `The ${data.humanPropName} property "${mismatchPropName}" ` +
`${ajvValidationErrorObj.message}`,
reasonCode: 'INVALID_TYPE'
reasonCode: 'INVALID_TYPE',
severity: SEVERITY.ERROR
};

switch (ajvValidationErrorObj.keyword) {

case 'additionalProperties':
mismatchObj.reasonCode = 'MISSING_IN_SCHEMA';
mismatchObj.severity = SEVERITY.WARNING;
break;

// currently not supported as OAS doesn't support this keyword
Expand All @@ -42,6 +51,7 @@ function ajvValidationError(ajvValidationErrorObj, data) {
mismatchObj.reasonCode = 'MISSING_IN_REQUEST';
mismatchObj.reason = `The ${data.humanPropName} property "${mismatchPropName}" should have required ` +
`property "${_.get(ajvValidationErrorObj, 'params.missingProperty')}"`;
mismatchObj.severity = SEVERITY.ERROR;
break;

// currently not supported as OAS doesn't support this keyword
Expand Down
63 changes: 45 additions & 18 deletions lib/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/sc
'authorization'
],

// Mismatch severities
SEVERITY = {
INFO: 'info',
LOG: 'log',
WARNING: 'warning',
ERROR: 'error'
},

crypto = require('crypto'),
DEFAULT_SCHEMA_UTILS = require('./30XUtils/schemaUtils30X'),
{ getRelatedFiles } = require('./relatedFiles'),
Expand Down Expand Up @@ -3368,7 +3376,8 @@ module.exports = {
transactionJsonPath: jsonPathPrefix,
schemaJsonPath: schemaPathPrefix,
reasonCode: 'INVALID_TYPE',
reason
reason,
severity: SEVERITY.ERROR
};

if (options.suggestAvailableFixes) {
Expand Down Expand Up @@ -3405,6 +3414,7 @@ module.exports = {
dataPath = dataPath.slice(1);
}

// To add more kinds of severities depending upon type of validation error
mismatchObj = _.assign({
property: property,
transactionJsonPath: jsonPathPrefix + formatDataPath(ajvError.instancePath),
Expand All @@ -3424,7 +3434,8 @@ module.exports = {
else {
mismatchObj = {
reason: `The ${humanPropName} didn\'t match the specified schema`,
reasonCode: 'INVALID_TYPE'
reasonCode: 'INVALID_TYPE',
severity: SEVERITY.ERROR
};

// assign proper reason codes for invalid body
Expand Down Expand Up @@ -3490,7 +3501,8 @@ module.exports = {
key: null,
actualValue: valueToUse,
suggestedValue: {} // suggest value to be object
}
},
severity: SEVERITY.ERROR
}]);
}
else {
Expand Down Expand Up @@ -3582,7 +3594,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix + `[${index}]`,
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The path variable "${pathVar.key}" was not found in the schema`
reason: `The path variable "${pathVar.key}" was not found in the schema`,
severity: SEVERITY.WARNING
});
}
return cb(null, mismatches);
Expand Down Expand Up @@ -3661,7 +3674,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix,
schemaJsonPath: pathVar.pathPrefix,
reasonCode,
reason
reason,
severity: SEVERITY.ERROR
};

if (options.suggestAvailableFixes) {
Expand Down Expand Up @@ -3755,7 +3769,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix + '.name',
schemaJsonPath: null,
reasonCode: 'INVALID_VALUE',
reason: 'The request name didn\'t match with specified schema'
reason: 'The request name didn\'t match with specified schema',
severity: SEVERITY.LOG
};

options.suggestAvailableFixes && (mismatchObj.suggestedFix = {
Expand Down Expand Up @@ -3907,7 +3922,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix + `[${index}]`,
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The query parameter ${pQuery.key} was not found in the schema`
reason: `The query parameter ${pQuery.key} was not found in the schema`,
severity: SEVERITY.WARNING
});
}
return cb(null, mismatches);
Expand Down Expand Up @@ -3954,7 +3970,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix,
schemaJsonPath: qp.pathPrefix + '[?(@.name==\'' + qp.name + '\')]',
reasonCode: 'MISSING_IN_REQUEST',
reason: `The required query parameter "${qp.name}" was not found in the transaction`
reason: `The required query parameter "${qp.name}" was not found in the transaction`,
severity: SEVERITY.ERROR
};

if (options.suggestAvailableFixes) {
Expand Down Expand Up @@ -4051,7 +4068,8 @@ module.exports = {
reasonCode: 'MISSING_IN_SCHEMA',
// Reason for missing in schema suggests that certain media type in req/res body is not present
reason: `The ${mismatchProperty === 'HEADER' ? 'request' : 'response'} body should have media type` +
` "${contentHeaderMediaType}"`
` "${contentHeaderMediaType}"`,
severity: SEVERITY.WARNING
});
}
}
Expand All @@ -4063,7 +4081,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix,
schemaJsonPath: schemaPathPrefix,
reasonCode: 'MISSING_IN_REQUEST',
reason: `The ${humanPropName} "Content-Type" was not found in the transaction`
reason: `The ${humanPropName} "Content-Type" was not found in the transaction`,
severity: SEVERITY.WARNING
};

if (options.suggestAvailableFixes) {
Expand Down Expand Up @@ -4102,7 +4121,8 @@ module.exports = {
schemaJsonPath: schemaPathPrefix,
reasonCode: 'INVALID_TYPE',
reason: `The ${humanPropName} "Content-Type" needs to be "${suggestedContentHeader}",` +
` but we found "${contentHeaderMediaType}" instead`
` but we found "${contentHeaderMediaType}" instead`,
severity: SEVERITY.WARNING
};

if (options.suggestAvailableFixes) {
Expand Down Expand Up @@ -4152,7 +4172,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix + `[${index}]`,
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The header ${pHeader.key} was not found in the schema`
reason: `The header ${pHeader.key} was not found in the schema`,
severity: SEVERITY.WARNING
});
}
return cb(null, mismatches);
Expand Down Expand Up @@ -4210,7 +4231,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix,
schemaJsonPath: header.pathPrefix + '[?(@.name==\'' + header.name + '\')]',
reasonCode: 'MISSING_IN_REQUEST',
reason: `The required header "${header.name}" was not found in the transaction`
reason: `The required header "${header.name}" was not found in the transaction`,
severity: SEVERITY.ERROR
};

if (options.suggestAvailableFixes) {
Expand Down Expand Up @@ -4267,7 +4289,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix + `[${index}]`,
schemaJsonPath: schemaPathPrefix + '.headers',
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The header ${pHeader.key} was not found in the schema`
reason: `The header ${pHeader.key} was not found in the schema`,
severity: SEVERITY.WARNING
});
}
return cb(null, mismatches);
Expand Down Expand Up @@ -4317,7 +4340,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix,
schemaJsonPath: schemaPathPrefix + '.headers[\'' + header.name + '\']',
reasonCode: 'MISSING_IN_REQUEST',
reason: `The required response header "${header.name}" was not found in the transaction`
reason: `The required response header "${header.name}" was not found in the transaction`,
severity: SEVERITY.ERROR
};

if (options.suggestAvailableFixes) {
Expand Down Expand Up @@ -4467,7 +4491,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix + `.urlencoded[${index}]`,
schemaJsonPath: null,
reasonCode: 'MISSING_IN_SCHEMA',
reason: `The Url Encoded body param "${uParam.key}" was not found in the schema`
reason: `The Url Encoded body param "${uParam.key}" was not found in the schema`,
severity: SEVERITY.WARNING
});
}
return cb(null, mismatches);
Expand Down Expand Up @@ -4531,7 +4556,8 @@ module.exports = {
transactionJsonPath: transactionPathPrefix + '.urlencoded',
schemaJsonPath: pathPrefix + '.properties[' + uParam.name + ']',
reasonCode: 'MISSING_IN_REQUEST',
reason: `The Url Encoded body param "${uParam.name}" was not found in the transaction`
reason: `The Url Encoded body param "${uParam.name}" was not found in the transaction`,
severity: SEVERITY.ERROR
};

if (options.suggestAvailableFixes) {
Expand Down Expand Up @@ -4957,7 +4983,8 @@ module.exports = {
schemaJsonPath,
reasonCode: 'MISSING_ENDPOINT',
reason: `The endpoint "${_.toUpper(pathKey)} ${schemaPath}" is missing in collection`,
endpoint: _.toUpper(pathKey) + ' ' + schemaPath
endpoint: _.toUpper(pathKey) + ' ' + schemaPath,
severity: SEVERITY.WARNING
};

if (options.suggestAvailableFixes) {
Expand Down