mirror of https://github.com/pelias/api.git
Browse Source
This middleware looks at the list of types that will be sent to Elasticsearch, if it's an empty array, it sends an error response before Elasticsearch is even quieried, because Elasticsearch interprets an empty type array as "search anything" rather than the intended "don't search anything".pull/221/head
Julian Simioni
9 years ago
4 changed files with 37 additions and 6 deletions
@ -0,0 +1,27 @@
|
||||
var types_helper = require( '../helper/types' ); |
||||
|
||||
/** |
||||
* Validate the types specified to be searched. |
||||
* |
||||
* Elasticsearch interprets an empty array of types as "search anything" rather |
||||
* than "search nothing", so in the case of an empty array, return an error |
||||
* message instead of searching at all. |
||||
*/ |
||||
function middleware(req, res, next) { |
||||
var types = types_helper(req.clean.types); |
||||
|
||||
if (types !== undefined && types.length !== undefined) { |
||||
if (types.length === 0) { |
||||
var err = 'You have specified both the `source` and `layers` ' + |
||||
'parameters in a combination that will return no results.'; |
||||
res.status(400); // 400 Bad Request
|
||||
return next(err); |
||||
} else { |
||||
req.clean.type = types; |
||||
} |
||||
} |
||||
|
||||
next(); |
||||
} |
||||
|
||||
module.exports = middleware; |
Loading…
Reference in new issue