Windshaft-cartodb/lib/cartodb/models/aggregation/aggregation-validator.js

94 lines
3.5 KiB
JavaScript
Raw Normal View History

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) {
2017-12-19 02:52:50 +08:00
return function validateIncludesValue (value, key, index) {
if (!validValues.includes(value)) {
2017-12-19 17:25:41 +08:00
const message = `Invalid ${key}. Valid values: ${validValues.join(', ')}`;
throw createLayerError(message, mapconfig, index);
}
};
2017-12-19 02:47:11 +08:00
};
2017-12-19 02:51:55 +08:00
module.exports.createPositiveNumberValidator = function (mapconfig) {
2017-12-19 02:52:50 +08:00
return function validatePositiveNumber (value, key, index) {
if (!Number.isFinite(value) || value <= 0) {
2017-12-19 17:25:41 +08:00
const message = `Invalid ${key}, should be a number greather than 0`;
throw createLayerError(message, mapconfig, index);
}
};
2017-12-19 02:47:11 +08:00
};
2017-12-19 03:42:26 +08:00
module.exports.createAggregationColumnsValidator = function (mapconfig, validAggregatedFunctions) {
2017-12-19 17:43:34 +08:00
const validateAggregationColumnNames = createAggregationColumnNamesValidator(mapconfig);
const validateAggregateFunction = createAggregateFunctionValidator(mapconfig, validAggregatedFunctions);
const validateAggregatedColumn = createAggregatedColumnValidator(mapconfig);
2017-12-19 03:42:26 +08:00
return function validateAggregationColumns (value, key, index) {
2017-12-19 17:43:34 +08:00
validateAggregationColumnNames(value, key, index);
validateAggregateFunction(value, key, index);
validateAggregatedColumn(value, key, index);
};
};
function createAggregationColumnNamesValidator(mapconfig) {
return function validateAggregationColumnNames (value, key, index) {
2017-12-19 03:42:26 +08:00
Object.keys(value).forEach((columnName) => {
if (columnName.length <= 0) {
2017-12-19 17:25:41 +08:00
const message = `Invalid column name, should be a non empty string`;
throw createLayerError(message, mapconfig, index);
2017-12-19 03:42:26 +08:00
}
2017-12-19 17:43:34 +08:00
});
};
}
2017-12-19 03:42:26 +08:00
2017-12-19 17:43:34 +08:00
function createAggregateFunctionValidator (mapconfig, validAggregatedFunctions) {
return function validateAggregateFunction (value, key, index) {
Object.keys(value).forEach((columnName) => {
2017-12-19 03:42:26 +08:00
const { aggregate_function } = value[columnName];
if (!validAggregatedFunctions.includes(aggregate_function)) {
2017-12-19 17:25:41 +08:00
const message = `Unsupported aggregation function ${aggregate_function},` +
` valid ones: ${validAggregatedFunctions.join(', ')}`;
throw createLayerError(message, mapconfig, index);
2017-12-19 03:42:26 +08:00
}
2017-12-19 17:43:34 +08:00
});
};
}
2017-12-19 03:42:26 +08:00
2017-12-19 17:43:34 +08:00
function createAggregatedColumnValidator (mapconfig) {
return function validateAggregatedColumn (value, key, index) {
Object.keys(value).forEach((columnName) => {
2017-12-19 03:42:26 +08:00
const { aggregated_column } = value[columnName];
if (typeof aggregated_column !== 'string' || aggregated_column <= 0) {
2017-12-19 17:25:41 +08:00
const message = `Invalid aggregated column, should be a non empty string`;
throw createLayerError(message, mapconfig, index);
2017-12-19 03:42:26 +08:00
}
});
};
2017-12-19 17:43:34 +08:00
}
2017-12-19 17:25:41 +08:00
function createLayerError(message, mapconfig, index) {
const error = new Error(message);
error.type = 'layer';
error.layer = {
id: mapconfig.getLayerId(index),
index: index,
type: mapconfig.layerType(index)
};
return error;
}