diff --git a/lib/cartodb/models/aggregation/aggregation-mapconfig.js b/lib/cartodb/models/aggregation/aggregation-mapconfig.js index 379d7739..6b525def 100644 --- a/lib/cartodb/models/aggregation/aggregation-mapconfig.js +++ b/lib/cartodb/models/aggregation/aggregation-mapconfig.js @@ -1,5 +1,10 @@ const MapConfig = require('windshaft').model.MapConfig; const aggregationQuery = require('./aggregation-query'); +const aggregationValidator = require('./aggregation-validator'); +const { + createNumberValidator, + createIncludesValueValidator + } = aggregationValidator; module.exports = class AggregationMapConfig extends MapConfig { static get PLACEMENTS () { @@ -117,49 +122,3 @@ module.exports = class AggregationMapConfig extends MapConfig { validate('threshold', numberValidator); } }; - -function aggregationValidator (mapconfig) { - return function validateProperty (prop, validator) { - for (let index = 0; index < mapconfig.getLayers().length; index++) { - const aggregation = mapconfig.getAggregation(index); - - if (aggregation === undefined || aggregation[prop] === undefined) { - continue; - } - - validator(aggregation[prop], prop, index); - } - }; -} - -function createIncludesValueValidator(mapconfig, validValues) { - return function validateIncludesValue (prop, key, index) { - if (!validValues.includes(prop)) { - const error = new Error(`Invalid ${key}. Valid values: ${validValues.join(', ')}`); - error.type = 'layer'; - error.layer = { - id: mapconfig.getLayerId(index), - index: index, - type: mapconfig.layerType(index) - }; - - throw error; - } - }; -} - -function createNumberValidator(mapconfig) { - return function validateNumber (prop, key, index) { - if (!Number.isFinite(prop) || prop <= 0) { - const error = new Error(`Invalid ${key}, should be a number greather than 0`); - error.type = 'layer'; - error.layer = { - id: mapconfig.getLayerId(index), - index: index, - type: mapconfig.layerType(index) - }; - - throw error; - } - }; -} diff --git a/lib/cartodb/models/aggregation/aggregation-validator.js b/lib/cartodb/models/aggregation/aggregation-validator.js new file mode 100644 index 00000000..fc2e9066 --- /dev/null +++ b/lib/cartodb/models/aggregation/aggregation-validator.js @@ -0,0 +1,47 @@ + + +module.exports = function aggregationValidator (mapconfig) { + return function validateProperty (prop, validator) { + for (let index = 0; index < mapconfig.getLayers().length; index++) { + const aggregation = mapconfig.getAggregation(index); + + if (aggregation === undefined || aggregation[prop] === undefined) { + continue; + } + + validator(aggregation[prop], prop, index); + } + }; +}; + +module.exports.createIncludesValueValidator = function (mapconfig, validValues) { + return function validateIncludesValue (prop, key, index) { + if (!validValues.includes(prop)) { + const error = new Error(`Invalid ${key}. Valid values: ${validValues.join(', ')}`); + error.type = 'layer'; + error.layer = { + id: mapconfig.getLayerId(index), + index: index, + type: mapconfig.layerType(index) + }; + + throw error; + } + }; +} + +module.exports.createNumberValidator = function (mapconfig) { + return function validateNumber (prop, key, index) { + if (!Number.isFinite(prop) || prop <= 0) { + const error = new Error(`Invalid ${key}, should be a number greather than 0`); + error.type = 'layer'; + error.layer = { + id: mapconfig.getLayerId(index), + index: index, + type: mapconfig.layerType(index) + }; + + throw error; + } + }; +}