Skip to content

Commit

Permalink
Don't use regex for locationbias validation
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenmcd committed Jul 9, 2018
1 parent 8428a8a commit 27fff4e
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions lib/apis/places.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,34 @@ exports.findPlace = {
timeout: v.optional(v.number)
}),
function(query) {
var latLng = /^([-+]?\d{1,2}([.]\d+)?),*([-+]?\d{1,3}([.]\d+)?)$/;
var parts = ['ipbias|point:', '|circle:([1-9]d*)+@', '|rectangle:', '\\|'];
var re = new RegExp(parts.join(latLng.source), 'g');
if (query.locationbias && !re.test(query.locationbias)) {
throw new v.InvalidValueError('invalid locationbias');
var isLatLng = function(latLng) {
latLng = latLng.split(',');
return latLng.length == 2 && !isNaN(latLng[0]) && !isNaN(latLng[1]);
};
var parts = query.locationbias.split(':');
switch (parts[0]) {
case 'point':
if (isLatLng(parts[parts.length - 1])) {
return query;
}
break;
case 'circle':
parts = parts[parts.length - 1].split('@');
if (!isNaN(parts[0]) && isLatLng(parts[parts.length - 1])) {
return query;
}
break;
case 'rectangle':
parts = parts[parts.length - 1].split('|');
if (parts.length == 2 && isLatLng(parts[0]) && isLatLng(parts[1])) {
return query;
}
break;
case 'ipbias':
case '':
return query;
}
return query;
throw new v.InvalidValueError('invalid locationbias');
}
])
};
Expand Down

0 comments on commit 27fff4e

Please sign in to comment.