-
-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: move template config to package.json #349
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
28ac5e5
feat: move template config to package.json
derberg f4433f1
fix: lint issues
derberg 1132b21
Merge branch 'master' into remove-tp-config
derberg 7311d6a
refactor: additional validation refactor to mach changes
derberg 7ef1bb0
fix: code smells from other pr I forgot about
derberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const templateConfigValidator = jest.genMockFromModule('../templateConfigValidator'); | ||
|
||
module.exports = templateConfigValidator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
const semver = require('semver'); | ||
const Ajv = require('ajv'); | ||
const { getGeneratorVersion } = require('./utils'); | ||
|
||
const ajv = new Ajv({ allErrors: true }); | ||
|
||
/** | ||
* Validates the template configuration. | ||
* | ||
* @param {Object} templateConfig Template configuration. | ||
* @param {Object} templateParams Params specified when running generator. | ||
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPIDocument object to use as source. | ||
* @return {Boolean} | ||
*/ | ||
module.exports.validateTemplateConfig = (templateConfig, templateParams, asyncapiDocument) => { | ||
const { parameters, supportedProtocols, conditionalFiles, generator } = templateConfig; | ||
|
||
validateConditionalFiles(conditionalFiles); | ||
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); | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Checks if template is compatible with the version of the generator that is used | ||
* @private | ||
* @param {String} generator Information about supported generator version that is part of the template configuration | ||
*/ | ||
function isTemplateCompatible(generator) { | ||
const generatorVersion = getGeneratorVersion(); | ||
if (typeof generator === 'string' && !semver.satisfies(generatorVersion, generator)) { | ||
throw new Error(`This template is not compatible with the current version of the generator (${generatorVersion}). This template is compatible with the following version range: ${generator}.`); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if parameters described in template configuration as required are passed to the generator | ||
* @private | ||
* @param {Object} configParams Parameters specified in template configuration | ||
* @param {Object} templateParams All parameters provided to generator | ||
*/ | ||
function isRequiredParamProvided(configParams, templateParams) { | ||
const missingParams = Object.keys(configParams || {}) | ||
.filter(key => configParams[key].required && !templateParams[key]); | ||
|
||
if (missingParams.length) { | ||
throw new Error(`This template requires the following missing params: ${missingParams}.`); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if parameters provided to generator is supported by the template | ||
* @private | ||
* @param {Object} configParams Parameters specified in template configuration | ||
* @param {Object} templateParams All parameters provided to generator | ||
*/ | ||
function isProvidedParameterSupported(configParams, templateParams) { | ||
const wrongParams = Object.keys(templateParams || {}).filter(key => !configParams || !configParams[key]); | ||
|
||
if (wrongParams.length) { | ||
console.warn(`Warning: This template doesn't have the following params: ${wrongParams}.`); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if given AsyncAPI document has servers with protocol that is supported by the template | ||
* @private | ||
* @param {Object} server Server object from AsyncAPI file | ||
* @param {String[]} supportedProtocols Supported protocols specified in template configuration | ||
* @param {String} paramsServerName Name of the server specified as a param for the generator | ||
*/ | ||
function isServerProtocolSupported(server, supportedProtocols, paramsServerName) { | ||
if (server && Array.isArray(supportedProtocols) && !supportedProtocols.includes(server.protocol())) { | ||
throw new Error(`Server "${paramsServerName}" uses the ${server.protocol()} protocol but this template only supports the following ones: ${supportedProtocols}.`); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if given AsyncAPI document has servers with protocol that is supported by the template | ||
* @private | ||
* @param {Object} server Server object from AsyncAPI file | ||
* @param {String} paramsServerName Name of the server specified as a param for the generator | ||
*/ | ||
function isServerProvidedInDocument(server, paramsServerName) { | ||
if (typeof paramsServerName === 'string' && !server) throw new Error(`Couldn't find server with name ${paramsServerName}.`); | ||
} | ||
|
||
/** | ||
* Checks if conditional files are specified properly in the template | ||
* @private | ||
* @param {Object} conditionalFiles conditions specified in the template config | ||
*/ | ||
function validateConditionalFiles(conditionalFiles) { | ||
if (typeof conditionalFiles === 'object') { | ||
const fileNames = Object.keys(conditionalFiles) || []; | ||
fileNames.forEach(fileName => { | ||
const def = conditionalFiles[fileName]; | ||
if (typeof def.subject !== 'string') throw new Error(`Invalid conditional file subject for ${fileName}: ${def.subject}.`); | ||
if (typeof def.validation !== 'object') throw new Error(`Invalid conditional file validation object for ${fileName}: ${def.validation}.`); | ||
conditionalFiles[fileName].validate = ajv.compile(conditionalFiles[fileName].validation); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the order of calling validation and removed it from here https://github.com/asyncapi/generator/pull/349/files#diff-b58e2f1cee2acff55355343774e6d576L646 as I could not come up with an explanation why we were calling validation twice.
also, it is no longer async as I think it should never be