From 0b78cf18a58db06d9f3c9e75b8a5bc4a71a5f251 Mon Sep 17 00:00:00 2001 From: Luis Tejeda <46000487+LuisTejedaS@users.noreply.github.com> Date: Wed, 15 Jun 2022 18:08:34 -0500 Subject: [PATCH] Validate if the input version is supported Validate if the input version is supported --- lib/common/versionUtils.js | 18 ++++++++++++++++- lib/schemaUtils.js | 5 ++++- test/unit/bundle.test.js | 28 +++++++++++++++++++++++++++ test/unit/versionUtils.test.js | 35 +++++++++++++++++++++++++++++++++- 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/lib/common/versionUtils.js b/lib/common/versionUtils.js index 415e86b..67b9c57 100644 --- a/lib/common/versionUtils.js +++ b/lib/common/versionUtils.js @@ -256,6 +256,21 @@ function isOAS31(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 = { getSpecVersion, getConcreteSchemaUtils, @@ -265,5 +280,6 @@ module.exports = { getVersionRegexBySpecificationVersion, SWAGGER_VERSION, VERSION_3_1, - isOAS31 + isOAS31, + validateSupportedVersion }; diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 0bd7a53..f1f71cf 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -6,7 +6,7 @@ const { ParseError } = require('./common/ParseError.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'), sdk = require('postman-collection'), schemaFaker = require('../assets/json-schema-faker.js'), @@ -4994,6 +4994,9 @@ module.exports = { if (processInput.data[0].path === '') { 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 }; diff --git a/test/unit/bundle.test.js b/test/unit/bundle.test.js index 9dfd9e1..b2f8fd2 100644 --- a/test/unit/bundle.test.js +++ b/test/unit/bundle.test.js @@ -1918,6 +1918,34 @@ describe('bundle files method - 3.0', function () { expect(res.result).to.be.true; 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() { diff --git a/test/unit/versionUtils.test.js b/test/unit/versionUtils.test.js index 4876f3d..fc574ce 100644 --- a/test/unit/versionUtils.test.js +++ b/test/unit/versionUtils.test.js @@ -1,7 +1,8 @@ const { getSpecVersion, filterOptionsByVersion, compareVersion, - getVersionRegexBySpecificationVersion } = require('../../lib/common/versionUtils'), + getVersionRegexBySpecificationVersion, + validateSupportedVersion } = require('../../lib/common/versionUtils'), expect = require('chai').expect; 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; + }); +}); +