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 1e21691 commit 8e28af9
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/verify-config.js
@@ -1,19 +1,22 @@
const {isString, isUndefined, isBoolean} = require('lodash');
const getError = require('./get-error');

module.exports = ({npmPublish, tarballDir, pkgRoot}) => {
const errors = [];
if (!isUndefined(npmPublish) && !isBoolean(npmPublish)) {
errors.push(getError('EINVALIDNPMPUBLISH', {npmPublish}));
}
const isNonEmptyString = value => isString(value) && value.trim();

if (!isUndefined(tarballDir) && !isString(tarballDir)) {
errors.push(getError('EINVALIDTARBALLDIR', {tarballDir}));
}
const VALIDATORS = {
npmPublish: isBoolean,
tarballDir: isNonEmptyString,
pkgRoot: isNonEmptyString,
};

if (!isUndefined(pkgRoot) && !isString(pkgRoot)) {
errors.push(getError('EINVALIDPKGROOT', {pkgRoot}));
}
module.exports = options => {
const errors = Object.entries(options).reduce(
(errors, [option, value]) =>
!isUndefined(value) && value !== false && !VALIDATORS[option](value)
? [...errors, getError(`EINVALID${option.toUpperCase()}`, {[option]: value})]
: errors,
[]
);

return errors;
};

0 comments on commit 8e28af9

Please sign in to comment.