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.
50 lines
1.7 KiB
50 lines
1.7 KiB
9 years ago
|
var geo_common = require ('./_geo_common');
|
||
9 years ago
|
var _ = require('lodash');
|
||
9 years ago
|
var defaults = require('../query/reverse_defaults');
|
||
9 years ago
|
var LAT_LON_IS_REQUIRED = true,
|
||
9 years ago
|
CIRCLE_IS_REQUIRED = false;
|
||
9 years ago
|
|
||
|
// validate inputs, convert types and apply defaults
|
||
9 years ago
|
module.exports = function sanitize( raw, clean ){
|
||
9 years ago
|
|
||
9 years ago
|
// error & warning messages
|
||
|
var messages = { errors: [], warnings: [] };
|
||
9 years ago
|
|
||
9 years ago
|
// helper function to determine if raw has a boundary.circle property
|
||
|
var hasBoundaryCircleField = function(field) {
|
||
|
return raw.hasOwnProperty('boundary.circle.' + field);
|
||
|
};
|
||
|
|
||
9 years ago
|
if (['lat', 'lon'].some(hasBoundaryCircleField)) {
|
||
|
messages.warnings.push('boundary.circle.lat/boundary.circle.lon are currently unsupported');
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
try {
|
||
9 years ago
|
// first verify that point.lat/point.lon are valid
|
||
9 years ago
|
geo_common.sanitize_point( 'point', clean, raw, LAT_LON_IS_REQUIRED );
|
||
9 years ago
|
|
||
9 years ago
|
// overwrite boundary.circle.lat/lon with point.lat/lon
|
||
|
raw['boundary.circle.lat'] = clean['point.lat'];
|
||
|
raw['boundary.circle.lon'] = clean['point.lon'];
|
||
|
|
||
|
// if no radius was passed, set the default
|
||
9 years ago
|
if ( _.isUndefined( raw['boundary.circle.radius'] ) ) {
|
||
9 years ago
|
// the default is small unless layers other than venue or address were explicitly specified
|
||
|
if (clean.layers && clean.layers.length > 0 && !_.includes(clean.layers, 'venue') && !_.includes(clean.layers, 'address')) {
|
||
|
raw['boundary.circle.radius'] = defaults['boundary:circle:radius:coarse'];
|
||
|
} else {
|
||
|
raw['boundary.circle.radius'] = defaults['boundary:circle:radius'];
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// santize the boundary.circle
|
||
9 years ago
|
geo_common.sanitize_circle( 'boundary.circle', clean, raw, CIRCLE_IS_REQUIRED );
|
||
9 years ago
|
|
||
9 years ago
|
}
|
||
|
catch (err) {
|
||
9 years ago
|
messages.errors.push( err.message );
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
return messages;
|
||
9 years ago
|
};
|