-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Jambo build validation hook (#229)
* Add Jambo build validation hook - Added another operation inside PageWriter to ensure template data is correct using TemplateDataValidator, which pass in the templateArgument to the theme's validation hook function if it exists. Throw error if theme's hook function return false indicating bad template data. J=SLAP-1369 TEST=auto Passed Jest test using default config format with/without missing field when pass to validation hook Co-authored-by: Yen Truong <ytruong@yext.com>
- Loading branch information
Showing
5 changed files
with
193 additions
and
1 deletion.
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,48 @@ | ||
const fs = require('file-system'); | ||
const UserError = require('../../errors/usererror'); | ||
const { isCustomError } = require('../../utils/errorutils'); | ||
const { info } = require('../../utils/logger'); | ||
|
||
/** | ||
* TemplateDataValidator is reponsible for checking data supplied to a page | ||
* using Theme's custom validation steps (if any). | ||
*/ | ||
module.exports = class TemplateDataValidator { | ||
constructor(templateDataValidationHook) { | ||
/** | ||
* The path to template data validation hook. | ||
* @type {string} | ||
*/ | ||
this._templateDataValidationHook = templateDataValidationHook; | ||
|
||
/** | ||
* Whether or not the file for template data validation hook exists | ||
* @type {boolean} | ||
*/ | ||
this._hasHook = fs.existsSync(this._templateDataValidationHook); | ||
} | ||
|
||
/** | ||
* Execute validation hook's function if file exists | ||
* | ||
* @param {Object} page | ||
* @param {string} page.pageName name of the current page | ||
* @param {Object} page.pageData template arguments for the current page | ||
* @throws {UserError} on failure to execute hook | ||
* @returns {boolean} whether or not to throw an exception on bad template arguments | ||
*/ | ||
validate({ pageName, pageData }) { | ||
if (!this._hasHook) { | ||
return true; | ||
} | ||
try { | ||
info(`Validating configuration for page "${pageName}".`); | ||
const validatorFunction = require(this._templateDataValidationHook); | ||
return validatorFunction(pageData); | ||
} catch (err) { | ||
const msg = | ||
`Error executing validation hook from ${this._templateDataValidationHook}: `; | ||
throw new UserError(msg, err.stack); | ||
} | ||
} | ||
} |
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,107 @@ | ||
const TemplateDataValidator = require('../../../src/commands/build/templatedatavalidator'); | ||
const path = require('path'); | ||
const UserError = require('../../../src/errors/usererror'); | ||
|
||
describe('TemplateDataValidator validates config data using hook properly', () => { | ||
const currentPageConfig = { | ||
url: 'examplePage.html', | ||
verticalKey: 'examplePage', | ||
pageTitle: 'Example Page', | ||
pageSettings: { search: { verticalKey: 'examplePage', defaultInitialSearch: '' } }, | ||
componentSettings: { | ||
prop: 'example1', | ||
}, | ||
verticalsToConfig: { | ||
examplePage: { | ||
prop: 'example2' | ||
} | ||
} | ||
}; | ||
const verticalConfigs = { | ||
page1: { | ||
config: { | ||
prop: 'example' | ||
} | ||
}, | ||
page2: { | ||
config: { | ||
prop: 'example2' | ||
} | ||
} | ||
}; | ||
const global_config = { | ||
sdkVersion: 'X.X', | ||
apiKey: 'exampleKey', | ||
experienceKey: 'slanswers', | ||
locale: 'en' | ||
}; | ||
const params = { | ||
example: 'param' | ||
}; | ||
const relativePath = '..'; | ||
const env = { | ||
envVar: 'envVar', | ||
}; | ||
|
||
it('does not throw an error with a correct config', () => { | ||
const templateDataValidationHook = path.resolve( | ||
__dirname, '../../fixtures/hooks/templatedatavalidator.js'); | ||
const templateData = { | ||
currentPageConfig, | ||
verticalConfigs : verticalConfigs, | ||
global_config : global_config, | ||
params : params, | ||
relativePath: relativePath, | ||
env: env | ||
}; | ||
|
||
const isValid = new TemplateDataValidator(templateDataValidationHook).validate({ | ||
pageName: 'examplePage', | ||
pageData: templateData | ||
}); | ||
expect(isValid).toEqual(true); | ||
}); | ||
|
||
it('throws an error when a field in config is missing', () => { | ||
const templateDataValidationHook = path.resolve( | ||
__dirname, '../../fixtures/hooks/templatedatavalidator.js'); | ||
const global_config_missing_key = {}; | ||
const templateData = { | ||
currentPageConfig, | ||
verticalConfigs : verticalConfigs, | ||
global_config : global_config_missing_key, | ||
params : params, | ||
relativePath: relativePath, | ||
env: env | ||
}; | ||
|
||
const isValid = new TemplateDataValidator(templateDataValidationHook).validate({ | ||
pageName: 'examplePage', | ||
pageData: templateData | ||
}); | ||
expect(isValid).toEqual(false); | ||
|
||
}); | ||
|
||
|
||
it('does not throw error, gracefully ignore missing config field in bad pages', () => { | ||
const templateDataValidationHook = path.resolve( | ||
__dirname, '../../fixtures/hooks/templatedatavalidator.js'); | ||
const params_missing_field = {}; | ||
const templateData = { | ||
currentPageConfig, | ||
verticalConfigs : verticalConfigs, | ||
global_config : global_config, | ||
params : params_missing_field, | ||
relativePath: relativePath, | ||
env: env | ||
}; | ||
|
||
const isValid = new TemplateDataValidator(templateDataValidationHook).validate({ | ||
pageName: 'examplePage', | ||
pageData: templateData | ||
}); | ||
expect(isValid).toEqual(true); | ||
}); | ||
|
||
}); |
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,18 @@ | ||
const { warn } = require('../../../src/utils/logger'); | ||
/** | ||
* A test data validator hook. | ||
* | ||
* @param {Object} pageData configuration(s) of a page template | ||
* @returns {boolean} false if validator should throw an error | ||
*/ | ||
module.exports = function (pageData) { | ||
if(!pageData["params"]["example"]) { | ||
warn('Missing Info: param example in config file(s)'); | ||
return true; //gracefully ignore missing param on page | ||
} | ||
if(!pageData["global_config"]["experienceKey"]) { | ||
warn('Missing Info: experienceKey in config file(s)'); | ||
return false; | ||
} | ||
return true; | ||
} |