Browse Source

fix(geo_common): check for invalid bbox where min=max

This condition will cause Elasticsearch to throw an error, we should
catch it outselves first.

The error is more friendly than the case where min>max, but still an
error.

Connects https://github.com/pelias/api/pull/1050
pull/1207/head
Julian Simioni 6 years ago
parent
commit
1553dfb103
No known key found for this signature in database
GPG Key ID: B9EEB0C6EE0910A1
  1. 2
      sanitizer/_geo_common.js
  2. 25
      test/unit/sanitizer/_geo_common.js

2
sanitizer/_geo_common.js

@ -48,7 +48,7 @@ function sanitize_bbox_min_max(raw, key_prefix) {
const max = parseFloat(raw[`${key_prefix}.max_${dimension}`]);
const min = parseFloat(raw[`${key_prefix}.min_${dimension}`]);
if (max < min) {
if (max <= min) {
throw new Error(`min_${dimension} is larger than max_${dimension} in ${key_prefix}`);
}
});

25
test/unit/sanitizer/_geo_common.js

@ -241,13 +241,12 @@ module.exports.tests.rect = function(test, common) {
};
var mandatory = false;
sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory );
t.equal(clean['boundary.rect.min_lat'], params['boundary.rect.min_lat'], 'min_lat approved');
t.equal(clean['boundary.rect.max_lat'], params['boundary.rect.max_lat'], 'min_lat approved');
t.equal(clean['boundary.rect.min_lon'], params['boundary.rect.min_lon'], 'min_lat approved');
t.equal(clean['boundary.rect.max_lon'], params['boundary.rect.max_lon'], 'min_lat approved');
t.throws( function(){
sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory );
});
t.end();
});
test('invalid rect - partially specified', function (t) {
var clean = {};
var params = {
@ -299,6 +298,22 @@ module.exports.tests.rect = function(test, common) {
t.end();
});
test('invalid rect - max/min equal', function (t) {
var clean = {};
var params = {
'boundary.rect.max_lat': 52,
'boundary.rect.max_lon': 14,
'boundary.rect.min_lat': 52,
'boundary.rect.min_lon': 12
};
var mandatory = false;
t.throws( function() {
sanitize.sanitize_rect( 'boundary.rect', clean, params, mandatory );
});
t.end();
});
test('invalid rect - out of range latitude', function(t) {
var clean = {};
var params = {

Loading…
Cancel
Save