Skip to content
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

use partials in validator #914

Merged
merged 6 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
tmeyer2115 marked this conversation as resolved.
Show resolved Hide resolved
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