Windshaft-cartodb/lib/api/middlewares/coordinates.js

44 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
2018-04-17 17:22:05 +08:00
const positiveIntegerNumberRegExp = /^\d+$/;
const integerNumberRegExp = /^-?\d+$/;
2018-04-17 17:04:03 +08:00
const invalidZoomMessage = function (zoom) {
2018-04-17 17:36:33 +08:00
return `Invalid zoom value (${zoom}). It should be an integer number greather than or equal to 0`;
2018-04-17 17:04:03 +08:00
};
const invalidCoordXMessage = function (x) {
return `Invalid coodinate 'x' value (${x}). It should be an integer number`;
};
const invalidCoordYMessage = function (y) {
2018-04-17 17:36:33 +08:00
return `Invalid coodinate 'y' value (${y}). It should be an integer number greather than or equal to 0`;
2018-04-17 17:04:03 +08:00
};
2018-04-17 17:04:03 +08:00
module.exports = function coordinates (validate = { z: true, x: true, y: true }) {
return function coordinatesMiddleware (req, res, next) {
const { z, x, y } = req.params;
2018-04-17 17:22:05 +08:00
if (validate.z && !positiveIntegerNumberRegExp.test(z)) {
2018-04-17 17:04:03 +08:00
const err = new Error(invalidZoomMessage(z));
err.http_status = 400;
return next(err);
}
2018-04-17 00:57:28 +08:00
// Negative values for x param are valid. The x param is wrapped
2018-04-17 17:22:05 +08:00
if (validate.x && !integerNumberRegExp.test(x)) {
2018-04-17 17:04:03 +08:00
const err = new Error(invalidCoordXMessage(x));
err.http_status = 400;
return next(err);
}
2018-04-17 17:22:05 +08:00
if (validate.y && !positiveIntegerNumberRegExp.test(y)) {
2018-04-17 17:04:03 +08:00
const err = new Error(invalidCoordYMessage(y));
err.http_status = 400;
return next(err);
}
next();
};
};