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 { module.exports = class ClusterBackend {
// TODO: reduce complexity // TODO: reduce complexity
// jshint maxcomplexity: 13 // jshint maxcomplexity: 10
getClusterFeatures (mapConfigProvider, params, callback) { getClusterFeatures (mapConfigProvider, params, callback) {
mapConfigProvider.getMapConfig((err, _mapConfig) => { mapConfigProvider.getMapConfig((err, _mapConfig) => {
if (err) { if (err) {
@ -77,10 +77,9 @@ module.exports = class ClusterBackend {
} }
if (expressions !== undefined) { if (expressions !== undefined) {
if (!isValidExpression(expressions)) { try {
const error = new Error( validateExpressions(expressions);
`Invalid aggregation input, expressions should be and object with valid functions` } catch (error) {
);
error.http_status = 400; error.http_status = 400;
error.type = 'layer'; error.type = 'layer';
error.subtype = 'aggregation'; error.subtype = 'aggregation';
@ -91,36 +90,6 @@ module.exports = class ClusterBackend {
return callback(error); 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; 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) { function isValidExpression (expressions) {
const invalidTypes = ['string', 'number', 'boolean']; const invalidTypes = ['string', 'number', 'boolean'];