mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
Merge pull request #554 from postmanlabs/fix/ValidateVersionBundle
Validate the version input
This commit is contained in:
@@ -253,6 +253,21 @@ function isSwagger(version) {
|
||||
return isSwagger;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
@@ -261,5 +276,6 @@ module.exports = {
|
||||
compareVersion,
|
||||
getVersionRegexBySpecificationVersion,
|
||||
SWAGGER_VERSION,
|
||||
VERSION_3_1
|
||||
VERSION_3_1,
|
||||
validateSupportedVersion
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
const { ParseError } = require('./common/ParseError.js');
|
||||
|
||||
const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/schemaUtilsCommon.js'),
|
||||
{ getConcreteSchemaUtils, SWAGGER_VERSION } = require('./common/versionUtils.js'),
|
||||
{ getConcreteSchemaUtils, SWAGGER_VERSION, validateSupportedVersion } = require('./common/versionUtils.js'),
|
||||
async = require('async'),
|
||||
sdk = require('postman-collection'),
|
||||
schemaFaker = require('../assets/json-schema-faker.js'),
|
||||
@@ -4986,6 +4986,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
|
||||
};
|
||||
|
||||
@@ -1898,6 +1898,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');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -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,36 @@ 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;
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user