fix validation

This commit is contained in:
Josh Harvey 2017-06-08 18:39:09 +03:00
parent 8a725a54f1
commit 15cc0dc975

View file

@ -32,7 +32,7 @@ function allMachineScopes (machineList, machineScope) {
return machineScopes return machineScopes
} }
function satisfiesRequireAny (config, cryptos, machineList, field, refFields) { function satisfiesRequire (config, cryptos, machineList, field, anyFields, allFields) {
const fieldCode = field.code const fieldCode = field.code
const scopes = allScopes( const scopes = allScopes(
@ -41,36 +41,23 @@ function satisfiesRequireAny (config, cryptos, machineList, field, refFields) {
) )
return scopes.every(scope => { return scopes.every(scope => {
const isEnabled = () => refFields.some(refField => { const isAnyEnabled = () => _.some(refField => {
return isScopeEnabled(config, cryptos, machineList, refField, scope) 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 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 const isValid = isRequired ? !isBlank : true
return isValid if (!isValid) {
}) pp('DEBUG103')({fieldCode, isBlank, isRequired})
} }
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
return isValid return isValid
}) })
@ -161,6 +148,8 @@ function ensureConstraints (config) {
}) })
} }
const pp = require('./pp')
function validateRequires (config) { function validateRequires (config) {
return fetchMachines() return fetchMachines()
.then(machineList => { .then(machineList => {
@ -174,9 +163,9 @@ function validateRequires (config) {
const refFieldsAny = _.map(_.partial(getField, group), field.enabledIfAny) const refFieldsAny = _.map(_.partial(getField, group), field.enabledIfAny)
const refFieldsAll = _.map(_.partial(getField, group), field.enabledIfAll) const refFieldsAll = _.map(_.partial(getField, group), field.enabledIfAll)
const isInvalid = !satisfiesRequire(config, cryptos, machineList, field, refFieldsAny, refFieldsAll)
return !satisfiesRequireAny(config, cryptos, machineList, field, refFieldsAny) && return isInvalid
!satisfiesRequireAll(config, cryptos, machineList, field, refFieldsAll)
}) })
}) })
}) })