-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move template config to package.json (#349)
- Loading branch information
Showing
9 changed files
with
299 additions
and
122 deletions.
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.