Move aggregation validation to its own module
This commit is contained in:
@@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
47
lib/cartodb/models/aggregation/aggregation-validator.js
Normal file
47
lib/cartodb/models/aggregation/aggregation-validator.js
Normal file
@@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user