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

128 lines
5.2 KiB
JavaScript
Raw Normal View History

'use strict';
module.exports = function aggregationValidator (mapconfig) {
2017-12-19 17:47:53 +08:00
return function validateProperty (key, validator) {
for (let index = 0; index < mapconfig.getLayers().length; index++) {
const aggregation = mapconfig.getAggregation(index);
2017-12-19 17:47:53 +08:00
if (aggregation === undefined || aggregation[key] === undefined) {
continue;
}
2017-12-19 17:47:53 +08:00
validator(aggregation[key], key, 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);
};
};
2018-03-22 00:01:32 +08:00
module.exports.createAggregationFiltersValidator = function (mapconfig, validParameters) {
return function validateAggregationFilters (value, key, index) {
const dims = mapconfig.getAggregation(index).dimensions || {};
const cols = mapconfig.getAggregation(index).columns || {};
const validKeys = Object.keys(dims).concat(Object.keys(cols));
Object.keys(value).forEach((filteredName) => {
// filteredName must be the name of either an aggregated column or a dimension in the same layer
if (!validKeys.includes(filteredName)) {
const message = `Invalid filtered column: ${filteredName}`;
throw createLayerError(message, mapconfig, index);
}
// The filter parameters must be valid
let filters = value[filteredName];
// a single filter or an array of filters (to be OR-combined) are accepted
if (!Array.isArray(filters)) {
filters = [filters];
}
filters.forEach(params => {
Object.keys(params).forEach(paramName => {
if (!validParameters.includes(paramName)) {
const message = `Invalid filter parameter name: ${paramName}`;
throw createLayerError(message, mapconfig, index);
}
});
// TODO: check parameter value (params[paramName]) to be of the correct type
});
// TODO: if multiple parameters within params check the combination is valid,
// i.e. one of the *less* parameters and one of the *greater* parameters.
});
};
};
2019-10-22 01:07:24 +08:00
function createAggregationColumnNamesValidator (mapconfig) {
2017-12-19 17:43:34 +08:00
return function validateAggregationColumnNames (value, key, index) {
2017-12-19 03:42:26 +08:00
Object.keys(value).forEach((columnName) => {
if (columnName.length <= 0) {
2019-10-22 01:07:24 +08:00
const message = 'Invalid column name, should be a non empty string';
2017-12-19 17:25:41 +08:00
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) => {
2019-11-14 18:36:47 +08:00
const { aggregate_function: aggregateFunction } = value[columnName];
2017-12-19 03:42:26 +08:00
2019-11-14 18:36:47 +08:00
if (!validAggregatedFunctions.includes(aggregateFunction)) {
const message = `Unsupported aggregation function ${aggregateFunction},` +
2017-12-19 17:25:41 +08:00
` 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) => {
2019-11-14 18:36:47 +08:00
const { aggregated_column: aggregatedColumn } = value[columnName];
2017-12-19 03:42:26 +08:00
2019-11-14 18:36:47 +08:00
if (typeof aggregatedColumn !== 'string' || aggregatedColumn <= 0) {
2019-10-22 01:07:24 +08:00
const message = 'Invalid aggregated column, should be a non empty string';
2017-12-19 17:25:41 +08:00
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
2019-10-22 01:07:24 +08:00
function createLayerError (message, mapconfig, index) {
2017-12-19 17:25:41 +08:00
const error = new Error(message);
error.type = 'layer';
error.layer = {
id: mapconfig.getLayerId(index),
index: index,
type: mapconfig.layerType(index)
};
return error;
}