Validate if the input version is supported

Validate if the input version is supported
This commit is contained in:
Luis Tejeda
2022-06-15 18:08:34 -05:00
parent 6e192043d3
commit 0b78cf18a5
4 changed files with 83 additions and 3 deletions

View File

@@ -256,6 +256,21 @@ function isOAS31(version) {
return compareVersion(version, VERSION_31.version); return compareVersion(version, VERSION_31.version);
} }
/**
* Validates if the input version is valid
* @param {string} version The current spec version
* @returns {boolean} True if the current version is supported
*/
function validateSupportedVersion(version) {
if (!version) {
return false;
}
let isValid = [DEFAULT_SPEC_VERSION, SWAGGER_VERSION, VERSION_3_1].find((supportedVersion) => {
return compareVersion(version, supportedVersion);
});
return isValid !== undefined;
}
module.exports = { module.exports = {
getSpecVersion, getSpecVersion,
getConcreteSchemaUtils, getConcreteSchemaUtils,
@@ -265,5 +280,6 @@ module.exports = {
getVersionRegexBySpecificationVersion, getVersionRegexBySpecificationVersion,
SWAGGER_VERSION, SWAGGER_VERSION,
VERSION_3_1, VERSION_3_1,
isOAS31 isOAS31,
validateSupportedVersion
}; };

View File

@@ -6,7 +6,7 @@
const { ParseError } = require('./common/ParseError.js'); const { ParseError } = require('./common/ParseError.js');
const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/schemaUtilsCommon.js'), const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/schemaUtilsCommon.js'),
{ getConcreteSchemaUtils, isSwagger } = require('./common/versionUtils.js'), { getConcreteSchemaUtils, isSwagger, validateSupportedVersion } = require('./common/versionUtils.js'),
async = require('async'), async = require('async'),
sdk = require('postman-collection'), sdk = require('postman-collection'),
schemaFaker = require('../assets/json-schema-faker.js'), schemaFaker = require('../assets/json-schema-faker.js'),
@@ -4994,6 +4994,9 @@ module.exports = {
if (processInput.data[0].path === '') { if (processInput.data[0].path === '') {
throw new Error('"Path" of the data element should be provided'); throw new Error('"Path" of the data element should be provided');
} }
if (processInput.specificationVersion && !validateSupportedVersion(processInput.specificationVersion)) {
throw new Error(`The provided version "${processInput.specificationVersion}" is not valid`);
}
}, },
MULTI_FILE_API_TYPE_ALLOWED_VALUE MULTI_FILE_API_TYPE_ALLOWED_VALUE
}; };

View File

@@ -1918,6 +1918,34 @@ describe('bundle files method - 3.0', function () {
expect(res.result).to.be.true; expect(res.result).to.be.true;
expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected); expect(JSON.stringify(res.output.data[0].bundledContent, null, 2)).to.be.equal(expected);
}); });
it('Should throw error when version is not correct', async function () {
let contentRoot = fs.readFileSync(compositeNot + '/root.yaml', 'utf8'),
input = {
type: 'multiFile',
specificationVersion: 'Anything',
rootFiles: [
{
path: '/root.yaml'
}
],
data: [
{
path: '/root.yaml',
content: contentRoot
}
],
options: {},
bundleFormat: 'JSON'
};
try {
await Converter.bundle(input);
}
catch (error) {
expect(error.message).to.equal('The provided version "Anything" is not valid');
}
});
}); });
describe('bundle files method - 2.0', function() { describe('bundle files method - 2.0', function() {

View File

@@ -1,7 +1,8 @@
const { getSpecVersion, const { getSpecVersion,
filterOptionsByVersion, filterOptionsByVersion,
compareVersion, compareVersion,
getVersionRegexBySpecificationVersion } = require('../../lib/common/versionUtils'), getVersionRegexBySpecificationVersion,
validateSupportedVersion } = require('../../lib/common/versionUtils'),
expect = require('chai').expect; expect = require('chai').expect;
describe('getSpecVersion', function() { describe('getSpecVersion', function() {
@@ -329,3 +330,35 @@ describe('getVersionRegexBySpecificationVersion method', function () {
}); });
}); });
describe('validateSupportedVersion method', function () {
it('should return true with version 3.0', function () {
const result = validateSupportedVersion('3.0');
expect(result).to.be.true;
});
it('should return true with version 2.0', function () {
const result = validateSupportedVersion('2.0');
expect(result).to.be.true;
});
it('should return true with version 3.1', function () {
const result = validateSupportedVersion('3.1');
expect(result).to.be.true;
});
it('should return false with version "any"', function () {
const result = validateSupportedVersion('any');
expect(result).to.be.false;
});
it('should return false with version ""', function () {
const result = validateSupportedVersion('');
expect(result).to.be.false;
});
it('should return false with version undefined', function () {
const result = validateSupportedVersion();
expect(result).to.be.false;
});
});