Move aggregation validation to its own module

This commit is contained in:
Daniel García Aubert
2017-12-18 19:17:43 +01:00
parent 8a48b96c53
commit fb03cd3424
2 changed files with 52 additions and 46 deletions

View File

@@ -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;
}
};
}

View 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;
}
};
}