Skip to content

Commit

Permalink
fix: factorise the verification code in a function
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Jun 27, 2018
1 parent f304bb0 commit 7300663
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7300663

Please sign in to comment.