From 6a7a4c52e3a253733bda14d03568c2c9dda781d4 Mon Sep 17 00:00:00 2001 From: Vishal Shingala Date: Fri, 6 Jan 2023 11:40:00 +0530 Subject: [PATCH 1/2] Added validation mismatch severities --- lib/schemaUtils.js | 66 +++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 232ca8662..6b8b4bc07 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -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'), @@ -3358,7 +3366,8 @@ module.exports = { transactionJsonPath: jsonPathPrefix, schemaJsonPath: schemaPathPrefix, reasonCode: 'INVALID_TYPE', - reason + reason, + severity: SEVERITY.ERROR }; if (options.suggestAvailableFixes) { @@ -3395,10 +3404,12 @@ 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), - schemaJsonPath: schemaPathPrefix + '.' + localSchemaPath + schemaJsonPath: schemaPathPrefix + '.' + localSchemaPath, + severity: SEVERITY.ERROR }, ajvValidationError(ajvError, { property, humanPropName })); if (options.suggestAvailableFixes) { @@ -3414,7 +3425,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 @@ -3480,7 +3492,8 @@ module.exports = { key: null, actualValue: valueToUse, suggestedValue: {} // suggest value to be object - } + }, + severity: SEVERITY.ERROR }]); } else { @@ -3572,7 +3585,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); @@ -3651,7 +3665,8 @@ module.exports = { transactionJsonPath: transactionPathPrefix, schemaJsonPath: pathVar.pathPrefix, reasonCode, - reason + reason, + severity: SEVERITY.ERROR }; if (options.suggestAvailableFixes) { @@ -3745,7 +3760,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 = { @@ -3897,7 +3913,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); @@ -3944,7 +3961,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) { @@ -4041,7 +4059,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 }); } } @@ -4053,7 +4072,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) { @@ -4092,7 +4112,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) { @@ -4142,7 +4163,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); @@ -4200,7 +4222,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) { @@ -4257,7 +4280,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); @@ -4307,7 +4331,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) { @@ -4455,7 +4480,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); @@ -4519,7 +4545,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) { @@ -4945,7 +4972,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) { From 1c28425a0a616123c48bdb564cd40fa23a302297 Mon Sep 17 00:00:00 2001 From: Vishal Shingala Date: Fri, 6 Jan 2023 15:32:58 +0530 Subject: [PATCH 2/2] Added validation mismatch severities in detail for detailed blob validation --- lib/ajValidation/ajvValidationError.js | 14 ++++++++++++-- lib/schemaUtils.js | 3 +-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/ajValidation/ajvValidationError.js b/lib/ajValidation/ajvValidationError.js index 64b84b28d..1de1aa90b 100644 --- a/lib/ajValidation/ajvValidationError.js +++ b/lib/ajValidation/ajvValidationError.js @@ -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 @@ -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 @@ -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 diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 6b8b4bc07..e36d9e678 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -3408,8 +3408,7 @@ module.exports = { mismatchObj = _.assign({ property: property, transactionJsonPath: jsonPathPrefix + formatDataPath(ajvError.instancePath), - schemaJsonPath: schemaPathPrefix + '.' + localSchemaPath, - severity: SEVERITY.ERROR + schemaJsonPath: schemaPathPrefix + '.' + localSchemaPath }, ajvValidationError(ajvError, { property, humanPropName })); if (options.suggestAvailableFixes) {