From 7300663d11da9d68f4151e2635d4755fe4acd879 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Wed, 27 Jun 2018 17:24:03 -0400 Subject: [PATCH] fix: factorise the verification code in a function --- lib/verify.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/verify.js b/lib/verify.js index 40b71935..5b2bee52 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -3,17 +3,23 @@ const AggregateError = require('aggregate-error'); const getError = require('./get-error'); const resolveConfig = require('./resolve-config'); -module.exports = pluginConfig => { - const {changelogFile, changelogTitle} = resolveConfig(pluginConfig); - const errors = []; +const isNonEmptyString = value => isString(value) && value.trim(); - if (!isUndefined(changelogFile) && !(isString(changelogFile) && changelogFile.trim())) { - errors.push(getError('EINVALIDCHANGELOGFILE', {changelogFile})); - } +const VALIDATORS = { + changelogFile: isNonEmptyString, + changelogTitle: isNonEmptyString, +}; - if (!isUndefined(changelogTitle) && !(isString(changelogTitle) && changelogTitle.trim())) { - errors.push(getError('EINVALIDCHANGELOGTITLE', {changelogTitle})); - } +module.exports = pluginConfig => { + const options = resolveConfig(pluginConfig); + + const errors = Object.entries(options).reduce( + (errors, [option, value]) => + !isUndefined(value) && value !== false && !VALIDATORS[option](value) + ? [...errors, getError(`EINVALID${option.toUpperCase()}`, {[option]: value})] + : errors, + [] + ); if (errors.length > 0) { throw new AggregateError(errors);