mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.7 KiB
128 lines
3.7 KiB
9 years ago
|
/**
|
||
8 years ago
|
* helper sanitizer methods for geo parameters
|
||
9 years ago
|
*/
|
||
9 years ago
|
var groups = require('./_groups'),
|
||
|
util = require('util'),
|
||
9 years ago
|
check = require('check-types'),
|
||
8 years ago
|
wrap = require('./wrap'),
|
||
9 years ago
|
_ = require('lodash');
|
||
10 years ago
|
|
||
|
/**
|
||
9 years ago
|
* Parse and validate rect parameter
|
||
10 years ago
|
*
|
||
9 years ago
|
* @param {string} key_prefix
|
||
10 years ago
|
* @param {object} clean
|
||
9 years ago
|
* @param {object} raw
|
||
|
* @param {bool} bbox_is_required
|
||
10 years ago
|
*/
|
||
9 years ago
|
function sanitize_rect( key_prefix, clean, raw, bbox_is_required ) {
|
||
9 years ago
|
// calculate full property names from the key_prefix
|
||
9 years ago
|
var properties = [ 'min_lat', 'max_lat', 'min_lon', 'max_lon' ].map(function(prop) {
|
||
|
return key_prefix + '.' + prop;
|
||
9 years ago
|
});
|
||
10 years ago
|
|
||
9 years ago
|
// sanitize the rect property group, this throws an exception if
|
||
|
// the group is not complete
|
||
9 years ago
|
var bbox_present;
|
||
|
if (bbox_is_required) {
|
||
|
bbox_present = groups.required(raw, properties);
|
||
|
} else {
|
||
|
bbox_present = groups.optional(raw, properties);
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
// don't bother checking individual elements if bbox is not required
|
||
|
// and not present
|
||
|
if (!bbox_present) { return; }
|
||
|
|
||
9 years ago
|
// check each property individually. now that it is known a bbox is present,
|
||
|
// all properties must exist, so pass the true flag for coord_is_required
|
||
9 years ago
|
properties.forEach(function(prop) {
|
||
9 years ago
|
sanitize_coord(prop, clean, raw, true);
|
||
9 years ago
|
});
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
/**
|
||
9 years ago
|
* Parse and validate circle parameter
|
||
10 years ago
|
*
|
||
9 years ago
|
* @param {string} key_prefix
|
||
10 years ago
|
* @param {object} clean
|
||
9 years ago
|
* @param {object} raw
|
||
|
* @param {bool} circle_is_required
|
||
10 years ago
|
*/
|
||
9 years ago
|
function sanitize_circle( key_prefix, clean, raw, circle_is_required ) {
|
||
9 years ago
|
// sanitize both a point and a radius if radius is present
|
||
|
// otherwise just sanittize the point
|
||
9 years ago
|
if( check.assigned( raw[ key_prefix + '.radius' ] ) ){
|
||
9 years ago
|
sanitize_coord( key_prefix + '.radius', clean, raw, true );
|
||
9 years ago
|
sanitize_point( key_prefix, clean, raw, true);
|
||
|
} else {
|
||
|
sanitize_point( key_prefix, clean, raw, circle_is_required);
|
||
10 years ago
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
/**
|
||
9 years ago
|
* Parse and validate point parameter
|
||
9 years ago
|
*
|
||
9 years ago
|
* @param {string} key_prefix
|
||
9 years ago
|
* @param {object} clean
|
||
9 years ago
|
* @param {object} raw
|
||
|
* @param {bool} point_is_required
|
||
9 years ago
|
*/
|
||
9 years ago
|
function sanitize_point( key_prefix, clean, raw, point_is_required ) {
|
||
8 years ago
|
|
||
9 years ago
|
// calculate full property names from the key_prefix
|
||
9 years ago
|
var properties = [ 'lat', 'lon'].map(function(prop) {
|
||
|
return key_prefix + '.' + prop;
|
||
9 years ago
|
});
|
||
9 years ago
|
|
||
9 years ago
|
// sanitize the rect property group, this throws an exception if
|
||
|
// the group is not complete
|
||
9 years ago
|
var point_present;
|
||
|
if (point_is_required) {
|
||
|
point_present = groups.required(raw, properties);
|
||
|
} else {
|
||
|
point_present = groups.optional(raw, properties);
|
||
|
}
|
||
|
|
||
|
// don't bother checking individual elements if point is not required
|
||
|
// and not present
|
||
|
if (!point_present) { return; }
|
||
|
|
||
9 years ago
|
// check each property individually. now that it is known a bbox is present,
|
||
|
// all properties must exist, so pass the true flag for coord_is_required
|
||
9 years ago
|
properties.forEach(function(prop) {
|
||
9 years ago
|
sanitize_coord(prop, clean, raw, true);
|
||
9 years ago
|
});
|
||
8 years ago
|
|
||
|
// normalize co-ordinates by wrapping around the poles
|
||
|
var normalized = wrap(clean[properties[0]], clean[properties[1]]);
|
||
|
clean[properties[0]] = normalized.lat;
|
||
|
clean[properties[1]] = normalized.lon;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
/**
|
||
|
* Validate lat,lon values
|
||
|
*
|
||
9 years ago
|
* @param {string} key - which key to validate
|
||
|
* @param {object} clean - cleaned parameters object
|
||
|
* @param {object} raw - the raw request object
|
||
9 years ago
|
* @param {bool} latlon_is_required
|
||
|
*/
|
||
9 years ago
|
function sanitize_coord( key, clean, raw, latlon_is_required ) {
|
||
|
var parsedValue = parseFloat( raw[key] );
|
||
8 years ago
|
|
||
9 years ago
|
if ( _.isFinite( parsedValue ) ) {
|
||
|
clean[key] = parsedValue;
|
||
9 years ago
|
}
|
||
|
else if (latlon_is_required) {
|
||
|
throw new Error( util.format( 'missing param \'%s\'', key ) );
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
module.exports = {
|
||
9 years ago
|
sanitize_rect: sanitize_rect,
|
||
9 years ago
|
sanitize_coord: sanitize_coord,
|
||
9 years ago
|
sanitize_circle: sanitize_circle,
|
||
|
sanitize_point: sanitize_point
|
||
9 years ago
|
};
|