Decouple validation

Decouple validation
This commit is contained in:
Luis Tejeda
2021-11-24 16:29:26 -06:00
committed by Erik Mendoza
parent 5bd0e47af5
commit c8cdf86563
5 changed files with 156 additions and 32 deletions

54
lib/inputValidation.js Normal file
View File

@@ -0,0 +1,54 @@
module.exports = {
/**
* Validate Spec to check if some of the required fields are present.
*
* @param {Object} spec OpenAPI spec
* @return {Object} Validation result
*/
validateSpec: function (spec) {
// Checking for the all the required properties in the specification
if (!spec.hasOwnProperty('openapi')) {
return {
result: false,
reason: 'Specification must contain a semantic version number of the OAS specification'
};
}
if (!spec.hasOwnProperty('paths')) {
return {
result: false,
reason: 'Specification must contain Paths Object for the available operational paths'
};
}
if (!spec.hasOwnProperty('info')) {
return {
result: false,
reason: 'Specification must contain an Info Object for the meta-data of the API'
};
}
if (!spec.info.hasOwnProperty('$ref')) {
if (!spec.info.hasOwnProperty('title')) {
return {
result: false,
reason: 'Specification must contain a title in order to generate a collection'
};
}
if (!spec.info.hasOwnProperty('version')) {
return {
result: false,
reason: 'Specification must contain a semantic version number of the API in the Info Object'
};
}
}
// Valid specification
return {
result: true,
openapi: spec
};
}
};