Skip to content

Commit

Permalink
feat: wrong params suggestions throw errors (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg authored Jun 1, 2020
1 parent ceceb36 commit e4b3258
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
39 changes: 19 additions & 20 deletions lib/templateConfigValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ module.exports.validateTemplateConfig = (templateConfig, templateParams, asyncap
isTemplateCompatible(generator);

isRequiredParamProvided(parameters, templateParams);
isProvidedParameterSupported(parameters, templateParams);

if (asyncapiDocument) {
const server = asyncapiDocument.server(templateParams.server);
isServerProvidedInDocument(server, templateParams.server);
isServerProtocolSupported(server, supportedProtocols, templateParams.server);
}

isProvidedParameterSupported(parameters, templateParams);

return true;
};

Expand Down Expand Up @@ -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];
}

/**
Expand All @@ -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}`);
}

/**
Expand Down
24 changes: 15 additions & 9 deletions test/templateConfigValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
Expand All @@ -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', () => {
Expand All @@ -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.');
});
Expand All @@ -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.');
Expand Down

0 comments on commit e4b3258

Please sign in to comment.