Skip to content

Commit

Permalink
use partials in validator (#914)
Browse files Browse the repository at this point in the history
Use the new partials param in templatedatavalidator instead of fs.existsSync.
This is slightly more robust, for weird cases when somebody registers a card
or universalsectiontemplate in an atypical folder (for example consulting).

J=SLAP-1489
TEST=manual

test that the validator will catch incorrect card and unviersalsectiontemplates
  • Loading branch information
oshi97 committed Aug 13, 2021
1 parent 95c5996 commit 8256040
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 136 deletions.
70 changes: 44 additions & 26 deletions hooks/templatedatavalidator.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
var fs = require('fs');
var path = require('path');
const path = require('path');
const { parseJamboConfig } = require('../commands/helpers/utils/jamboconfigutils');
const { error } = require('../commands/helpers/utils/logger');

/**
* Validates page template's data configuration
* Validates the template data and partials used during jambo build.
*
* @param {Object} pageData data that gets passed into a page template
* @param {Object<string, Function|string>} partials
* mapping of partial name to partial. Handlebars
* converts partials from strings to Functions when they are used.
* @returns {boolean} false if validator should throw an error
*/
module.exports = function (pageData) {
module.exports = function (pageData, partials) {
const jamboConfig = parseJamboConfig();
const validatorResults = [
isGlobalConfigValid(pageData.global_config),
isPageVerticalConfigValid(pageData, jamboConfig)
isPageVerticalConfigValid(pageData, jamboConfig, partials)
];
const isValid = validatorResults.every(result => result);
return isValid;
Expand All @@ -37,20 +40,21 @@ function isGlobalConfigValid(globalConfig) {
*
* @param {Object} pageData
* @param {Object} jamboConfig
* @param {Object<string, Function|string>} partials mapping of partial name to partial
* @returns {boolean}
*/
function isPageVerticalConfigValid(pageData, jamboConfig) {
function isPageVerticalConfigValid(pageData, jamboConfig, partials) {
const themeDirectory = path.join(jamboConfig.dirs.themes, jamboConfig.defaultTheme);
return Object.keys(pageData.verticalsToConfig)
.map(key => {
if (key === 'Universal') {
return isAllVerticalConfigsValid(pageData.verticalConfigs, jamboConfig);
return isAllVerticalConfigsValid(pageData.verticalConfigs, jamboConfig, partials);
}
const universalSectionTemplate = pageData.verticalsToConfig[key].universalSectionTemplate;
const cardType = pageData.verticalsToConfig[key].cardType;
const validatorResults = [
isUniversalSectionTemplateValid(key, themeDirectory, universalSectionTemplate),
isCardTypeValid(key, themeDirectory, cardType)
isUniversalSectionTemplateValid(key, themeDirectory, universalSectionTemplate, partials),
isCardTypeValid(key, themeDirectory, cardType, partials)
];
return validatorResults.every(result => result);
})
Expand All @@ -59,19 +63,19 @@ function isPageVerticalConfigValid(pageData, jamboConfig) {

/**
* If universalsectiontemplate is defined, check whether the corresponding file exists in theme
*
*
* @param {string} verticalName
* @param {string} themeDir
* @param {string} template
* @param {Object<string, Function|string>} partials mapping of partial name to partial
* @returns {boolean}
*/
function isUniversalSectionTemplateValid(verticalName, themeDir, template) {
if (template) {
function isUniversalSectionTemplateValid(verticalName, themeDir, template, partials) {
const partialName = `universalsectiontemplates/${template}`;
if (template && !partials[partialName]) {
const universalSectionPath = path.join(themeDir, 'universalsectiontemplates/', template + '.hbs');
if (!fs.existsSync(universalSectionPath)) {
error(`Invalid universalSectionTemplate: can't find "${template}" at the expected path "${universalSectionPath}" for vertical "${verticalName}".`);
return false;
}
logThatPartialDNE(partialName, verticalName, [universalSectionPath]);
return false;
}
return true;
}
Expand All @@ -82,16 +86,16 @@ function isUniversalSectionTemplateValid(verticalName, themeDir, template) {
* @param {string} verticalName
* @param {string} themeDir
* @param {string} cardType
* @param {Object<string, Function|string>} partials mapping of partial name to partial
* @returns {boolean}
*/
function isCardTypeValid(verticalName, themeDir, cardType) {
if (cardType) {
const cardTypePath = path.join(themeDir, 'cards/', cardType);
const customCardTypePath = path.join('cards/', cardType);
if (!fs.existsSync(cardTypePath) && !fs.existsSync(customCardTypePath)) {
error(`Invalid cardType: can't find "${cardType}" at at the expected paths "${cardTypePath}" or "${customCardTypePath}" for vertical "${verticalName}".`);
return false;
}
function isCardTypeValid(verticalName, themeDir, cardType, partials) {
const partial = `cards/${cardType}/component`;
if (cardType && !partials[partial]) {
const cardTypePath = path.join(themeDir, 'cards/', cardType, 'component.js');
const customCardTypePath = path.join('cards/', cardType, 'component.js');
logThatPartialDNE(partial, verticalName, [cardTypePath, customCardTypePath]);
return false;
}
return true;
}
Expand All @@ -101,15 +105,29 @@ function isCardTypeValid(verticalName, themeDir, cardType) {
*
* @param {Object} verticalConfigs
* @param {Object} jamboConfig
* @param {Object<string, Function|string>} partials mapping of partial name to partial
* @returns {boolean}
*/
function isAllVerticalConfigsValid(verticalConfigs, jamboConfig) {
function isAllVerticalConfigsValid(verticalConfigs, jamboConfig, partials) {
if (!verticalConfigs) {
return true;
}
return Object.keys(verticalConfigs)
.map(key => {
return isPageVerticalConfigValid(verticalConfigs[key], jamboConfig);
return isPageVerticalConfigValid(verticalConfigs[key], jamboConfig, partials);
})
.every(result => result);
}

function logThatPartialDNE(partialName, verticalName, defaultLocations = []) {
let msg = `Cannot find partial \`${partialName}\` for vertical \`${verticalName}\`.`
if (defaultLocations.length === 1) {
msg += `\nBy default this partial is located in ${defaultLocations[0]}`;
} else if (defaultLocations.length > 1) {
const lastLocation = defaultLocations[defaultLocations.length - 1];
const commaSeparatedLocations = defaultLocations.slice(0, -1).join(', ');
const parsedLocations = `${commaSeparatedLocations} or ${lastLocation}`
msg += `\nBy default this partial is located in ${parsedLocations}`;
}
error(msg);
}
Loading

0 comments on commit 8256040

Please sign in to comment.