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

View File

@@ -0,0 +1,39 @@
module.exports = {
/**
* Validate Spec to check if some of the required fields are present.
* OpenAPI 3.1 only openapi and info are always required,
* but the document must also contain at least one of paths or webhooks or components.
* @param {Object} spec OpenAPI spec
* @return {Object} Validation result
*/
validateSpec: function (spec) {
if (!spec.hasOwnProperty('openapi')) {
return {
result: false,
reason: 'Specification must contain a semantic version number of the OAS specification'
};
}
if (!spec.hasOwnProperty('info')) {
return {
result: false,
reason: 'Specification must contain an Info Object for the meta-data of the API'
};
}
if (!spec.hasOwnProperty('paths') && !spec.hasOwnProperty('webhooks') && !spec.hasOwnProperty('components')) {
return {
result: false,
reason: 'Specification must contain either Paths, Webhooks or Components sections'
};
}
return {
result: true,
openapi: spec
};
}
};

View File

@@ -0,0 +1,11 @@
const inputValidation31X = require('./inputValidation31X'),
schemaUtilsCommon = require('../common/schemaUtilsCommon');
module.exports = {
parseSpec: function (openApiSpec) {
return schemaUtilsCommon.parseSpec(openApiSpec, inputValidation31X);
}
};

View File

@@ -0,0 +1,49 @@
/**
* This file contains util functions that are common between versions
*/
const parse = require('../parse.js');
module.exports = {
/**
* Parses an OAS string/object as a YAML or JSON
* @param {YAML/JSON} openApiSpec - The OAS 3.x specification specified in either YAML or JSON
* @param {object} inputValidation - Concrete validator according to version
* @returns {Object} - Contains the parsed JSON-version of the OAS spec, or an error
* @no-unit-test
*/
parseSpec: function (openApiSpec, inputValidation) {
var openApiObj = openApiSpec,
obj,
rootValidation;
// If the open api specification is a string could be YAML or JSON
if (typeof openApiSpec === 'string') {
obj = parse.getOasObject(openApiSpec);
if (obj.result) {
openApiObj = obj.oasObject;
}
else {
return obj;
}
}
// spec is a valid JSON object at this point
// Validate the root level object for semantics
rootValidation = inputValidation.validateSpec(openApiObj);
if (!rootValidation.result) {
return {
result: false,
reason: rootValidation.reason
};
}
// Valid openapi root object
return {
result: true,
openapi: rootValidation.openapi
};
}
};

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
};
}
};

View File

@@ -6,7 +6,6 @@
const async = require('async'),
sdk = require('postman-collection'),
schemaFaker = require('../assets/json-schema-faker.js'),
parse = require('./parse.js'),
deref = require('./deref.js'),
_ = require('lodash'),
xmlFaker = require('./xmlSchemaFaker.js'),
@@ -16,6 +15,8 @@ const async = require('async'),
defaultOptions = require('../lib/options.js').getOptions('use'),
{ Node, Trie } = require('./trie.js'),
{ validateSchema } = require('./ajvValidation'),
inputValidation = require('./inputValidation'),
schemaUtilsCommon = require('./common/schemaUtilsCommon'),
SCHEMA_FORMATS = {
DEFAULT: 'default', // used for non-request-body data and json
XML: 'xml' // used for request-body XMLs
@@ -447,37 +448,7 @@ module.exports = {
* @no-unit-test
*/
parseSpec: function (openApiSpec) {
var openApiObj = openApiSpec,
obj,
rootValidation;
// If the open api specification is a string could be YAML or JSON
if (typeof openApiSpec === 'string') {
obj = parse.getOasObject(openApiSpec);
if (obj.result) {
openApiObj = obj.oasObject;
}
else {
return obj;
}
}
// spec is a valid JSON object at this point
// Validate the root level object for semantics
rootValidation = parse.validateSpec(openApiObj);
if (!rootValidation.result) {
return {
result: false,
reason: rootValidation.reason
};
}
// Valid openapi root object
return {
result: true,
openapi: rootValidation.openapi
};
return schemaUtilsCommon.parseSpec(openApiSpec, inputValidation);
},
/**