Skip to content

Commit

Permalink
Added support for reporting user errors with validation
Browse files Browse the repository at this point in the history
  • Loading branch information
VShingala committed May 9, 2023
1 parent 54b02fa commit 4428f77
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
9 changes: 5 additions & 4 deletions lib/common/generateValidationError.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const Ajv = require('ajv-draft-04'),
openapi3Schema = require('../../assets/openapi3Schema.json'),
swagger2Schema = require('../../assets/swagger2Schema.json'),
openapi3Utils = require('../30XUtils/schemaUtils30X'),
swagger2Utils = require('../swaggerUtils/schemaUtilsSwagger');
swagger2Utils = require('../swaggerUtils/schemaUtilsSwagger'),
{ jsonPointerDecodeAndReplace } = require('../jsonPointer');

/**
* Constructs Error message from given AJV Validation Error object.
Expand All @@ -13,16 +14,16 @@ const Ajv = require('ajv-draft-04'),
* @returns {String} - Error message constructed (To be displayed as error message for invalid specs)
*/
function constructErrorMessage (ajvError) {
let message = 'Provided API Specification is invalid, ';
let message = 'Provided API Specification is invalid. ';

message += ajvError.message;

if (ajvError.params) {
message += ' ' + JSON.stringify(ajvError.params);
}

if (ajvError.instancePath) {
message += ` at "${ajvError.instancePath}"`;
if (typeof ajvError.instancePath === 'string') {
message += ` at "${jsonPointerDecodeAndReplace(ajvError.instancePath)}"`;
}

return message;
Expand Down
16 changes: 11 additions & 5 deletions lib/schemapack.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ class SchemaPack {

try {
// We only convert if swagger is found otherwise this.openapi remains the same
return convertToOAS30IfSwagger(getConcreteSchemaUtils(this.input), this.openapi, (error, convertedOpenAPI) => {
if (error) {
return convertToOAS30IfSwagger(getConcreteSchemaUtils(this.input), this.openapi, (err, convertedOpenAPI) => {
if (err) {
const error = generateError(this.openapi, _.get(this.validationResult, 'specificationVersion'), err);

return callback(error);
}

Expand Down Expand Up @@ -310,8 +312,10 @@ class SchemaPack {

try {
// We only convert if swagger is found otherwise this.openapi remains the same
convertToOAS30IfSwagger(concreteUtils, this.openapi, (error, newOpenapi) => {
if (error) {
convertToOAS30IfSwagger(concreteUtils, this.openapi, (err, newOpenapi) => {
if (err) {
const error = generateError(this.openapi, _.get(this.validationResult, 'specificationVersion'), err);

return callback(error);
}

Expand Down Expand Up @@ -408,7 +412,9 @@ class SchemaPack {
}
}
catch (e) {
return callback(e);
const error = generateError(this.openapi, _.get(this.validationResult, 'specificationVersion'), e);

return callback(error);
}

collectionJSON = generatedStore.collection.toJSON();
Expand Down

0 comments on commit 4428f77

Please sign in to comment.