diff --git a/lib/templateConfigValidator.js b/lib/templateConfigValidator.js index 033a2942f..c2a2b0643 100644 --- a/lib/templateConfigValidator.js +++ b/lib/templateConfigValidator.js @@ -20,7 +20,6 @@ module.exports.validateTemplateConfig = (templateConfig, templateParams, asyncap isTemplateCompatible(generator); isRequiredParamProvided(parameters, templateParams); - isProvidedParameterSupported(parameters, templateParams); if (asyncapiDocument) { const server = asyncapiDocument.server(templateParams.server); @@ -28,6 +27,8 @@ module.exports.validateTemplateConfig = (templateConfig, templateParams, asyncap isServerProtocolSupported(server, supportedProtocols, templateParams.server); } + isProvidedParameterSupported(parameters, templateParams); + return true; }; @@ -61,24 +62,16 @@ function isRequiredParamProvided(configParams, templateParams) { /** * Provides a hint for a user about correct parameter name. * @private - * @param {Object} wrongParams Incorrectly written parameters + * @param {Object} wrongParam Incorrectly written parameter * @param {Object} configParams Parameters specified in template configuration */ -function provideAdvice(wrongParams, configParams) { - const suggestion = (p) => { - const sortInt = (a, b) => { - return a[0] - b[0]; - }; - const arr = Object.keys(configParams).map(param => [levenshtein(p, param), param]); - - return arr.sort(sortInt)[0][1]; +function getParamSuggestion(wrongParam, configParams) { + const sortInt = (a, b) => { + return a[0] - b[0]; }; - - if (configParams) { - wrongParams.forEach(wp => console.warn(`Did you mean "${suggestion(wp)}"?`)); - } else { - console.warn('This template doesn\'t have any params!'); - } + const arr = Object.keys(configParams).map(param => [levenshtein(wrongParam, param), param]); + + return arr.sort(sortInt)[0][1]; } /** @@ -89,11 +82,17 @@ function provideAdvice(wrongParams, configParams) { */ function isProvidedParameterSupported(configParams, templateParams) { const wrongParams = Object.keys(templateParams || {}).filter(key => !configParams || !configParams[key]); + + if (!wrongParams.length) return; + if (!configParams) throw new Error('This template doesn\'t have any params.'); - if (wrongParams.length) { - console.warn(`Warning: This template doesn't have the following params: ${wrongParams}.`); - provideAdvice(wrongParams, configParams); - } + let suggestionsString = ''; + + wrongParams.forEach(wp => { + suggestionsString += `\nDid you mean "${getParamSuggestion(wp,configParams)}" instead of "${wp}"?`; + }); + + throw new Error(`This template doesn't have the following params: ${wrongParams}.${suggestionsString}`); } /** diff --git a/test/templateConfigValidator.test.js b/test/templateConfigValidator.test.js index 0de153511..beaf22565 100644 --- a/test/templateConfigValidator.test.js +++ b/test/templateConfigValidator.test.js @@ -49,7 +49,6 @@ describe('Template Configuration Validator', () => { }); it('Validation throw error if provided param is not in the list of params supported by the template', () => { - console.warn = jest.fn(); const templateParams = { tsets: 'myTest' }; @@ -63,9 +62,7 @@ describe('Template Configuration Validator', () => { } } }; - validateTemplateConfig(templateConfig, templateParams); - expect(console.warn).toHaveBeenCalledWith('Warning: This template doesn\'t have the following params: tsets.'); - expect(console.warn).toHaveBeenCalledWith('Did you mean "test"?'); + expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('This template doesn\'t have the following params: tsets.\nDid you mean \"test\" instead of \"tsets\"?'); }); it('Validation throw error if provided param is not supported by the template as template has no params specified', () => { @@ -75,16 +72,20 @@ describe('Template Configuration Validator', () => { }; const templateConfig = {}; - validateTemplateConfig(templateConfig, templateParams); - expect(console.warn).toHaveBeenCalledWith('Warning: This template doesn\'t have the following params: test1.'); - expect(console.warn).toHaveBeenCalledWith('This template doesn\'t have any params!'); + expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('This template doesn\'t have any params.'); }); it('Validation throw error if specified server is not in asyncapi document', () => { const templateParams = { server: 'myserver' }; - const templateConfig = {}; + const templateConfig = { + parameters: { + server: { + description: '' + } + } + }; expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Couldn\'t find server with name myserver.'); }); @@ -94,7 +95,12 @@ describe('Template Configuration Validator', () => { server: 'dummy-mqtt' }; const templateConfig = { - supportedProtocols: ['myprotocol'] + supportedProtocols: ['myprotocol'], + parameters: { + server: { + description: '' + } + } }; expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Server \"dummy-mqtt\" uses the mqtt protocol but this template only supports the following ones: myprotocol.');