Reduce complexity by extracting function to validate expressions

This commit is contained in:
Daniel García Aubert 2019-03-11 17:25:29 +01:00
parent 8051dc5110
commit 49104a6add

View File

@ -7,7 +7,7 @@ const AggregationMapConfig = require('../models/aggregation/aggregation-mapconfi
module.exports = class ClusterBackend {
// TODO: reduce complexity
// jshint maxcomplexity: 13
// jshint maxcomplexity: 10
getClusterFeatures (mapConfigProvider, params, callback) {
mapConfigProvider.getMapConfig((err, _mapConfig) => {
if (err) {
@ -43,7 +43,7 @@ module.exports = class ClusterBackend {
let { aggregation } = params;
if ( aggregation !== undefined) {
if (aggregation !== undefined) {
try {
aggregation = JSON.parse(aggregation);
} catch (err) {
@ -77,10 +77,9 @@ module.exports = class ClusterBackend {
}
if (expressions !== undefined) {
if (!isValidExpression(expressions)) {
const error = new Error(
`Invalid aggregation input, expressions should be and object with valid functions`
);
try {
validateExpressions(expressions);
} catch (error) {
error.http_status = 400;
error.type = 'layer';
error.subtype = 'aggregation';
@ -91,36 +90,6 @@ module.exports = class ClusterBackend {
return callback(error);
}
for (const { aggregate_function, aggregated_column } of Object.values(expressions)) {
if (typeof aggregated_column !== 'string') {
const error = new Error(`Invalid aggregation input, aggregated column should be an string`);
error.http_status = 400;
error.type = 'layer';
error.subtype = 'aggregation';
error.layer = {
index: layerIndex,
type: layer.type
};
return callback(error);
}
if (typeof aggregate_function !== 'string') {
const error = new Error(
`Invalid aggregation input, aggregate function should be an string`
);
error.http_status = 400;
error.type = 'layer';
error.subtype = 'aggregation';
error.layer = {
index: layerIndex,
type: layer.type
};
return callback(error);
}
}
}
}
@ -264,6 +233,22 @@ function hasColumns (columns) {
return Array.isArray(columns) && columns.length;
}
function validateExpressions (expressions) {
if (!isValidExpression(expressions)) {
throw new Error(`Invalid aggregation input, expressions should be and object with valid functions`);
}
for (const { aggregate_function, aggregated_column } of Object.values(expressions)) {
if (typeof aggregated_column !== 'string') {
throw new Error(`Invalid aggregation input, aggregated column should be an string`);
}
if (typeof aggregate_function !== 'string') {
throw new Error(`Invalid aggregation input, aggregate function should be an string`);
}
}
}
function isValidExpression (expressions) {
const invalidTypes = ['string', 'number', 'boolean'];