diff --git a/lib/config-validate.js b/lib/config-validate.js index a823301e..13103da1 100644 --- a/lib/config-validate.js +++ b/lib/config-validate.js @@ -32,7 +32,7 @@ function allMachineScopes (machineList, machineScope) { return machineScopes } -function satisfiesRequireAny (config, cryptos, machineList, field, refFields) { +function satisfiesRequire (config, cryptos, machineList, field, anyFields, allFields) { const fieldCode = field.code const scopes = allScopes( @@ -41,36 +41,23 @@ function satisfiesRequireAny (config, cryptos, machineList, field, refFields) { ) return scopes.every(scope => { - const isEnabled = () => refFields.some(refField => { + const isAnyEnabled = () => _.some(refField => { return isScopeEnabled(config, cryptos, machineList, refField, scope) - }) + }, anyFields) + + const areAllEnabled = () => _.every(refField => { + return isScopeEnabled(config, cryptos, machineList, refField, scope) + }, allFields) const isBlank = _.isNil(configManager.scopedValue(scope[0], scope[1], fieldCode, config)) - const isRequired = refFields.length === 0 || isEnabled() + const isRequired = (_.isEmpty(anyFields) || isAnyEnabled()) && + (_.isEmpty(allFields) || areAllEnabled()) const isValid = isRequired ? !isBlank : true - return isValid - }) -} - -function satisfiesRequireAll (config, cryptos, machineList, field, refFields) { - const fieldCode = field.code - - const scopes = allScopes( - allCryptoScopes(cryptos, field.cryptoScope), - allMachineScopes(machineList, field.machineScope) - ) - - return scopes.every(scope => { - const isEnabled = () => _.isEmpty(refFields) || refFields.every(refField => { - return isScopeEnabled(config, cryptos, machineList, refField, scope) - }) - - const isBlank = _.isNil(configManager.scopedValue(scope[0], scope[1], fieldCode, config)) - const isRequired = refFields.length === 0 || isEnabled() - - const isValid = isRequired ? !isBlank : true + if (!isValid) { + pp('DEBUG103')({fieldCode, isBlank, isRequired}) + } return isValid }) @@ -161,6 +148,8 @@ function ensureConstraints (config) { }) } +const pp = require('./pp') + function validateRequires (config) { return fetchMachines() .then(machineList => { @@ -174,9 +163,9 @@ function validateRequires (config) { const refFieldsAny = _.map(_.partial(getField, group), field.enabledIfAny) const refFieldsAll = _.map(_.partial(getField, group), field.enabledIfAll) + const isInvalid = !satisfiesRequire(config, cryptos, machineList, field, refFieldsAny, refFieldsAll) - return !satisfiesRequireAny(config, cryptos, machineList, field, refFieldsAny) && - !satisfiesRequireAll(config, cryptos, machineList, field, refFieldsAll) + return isInvalid }) }) })