Skip to content

Commit

Permalink
fix(lib): Ensure match and match_phrase handle empty Variables
Browse files Browse the repository at this point in the history
We want the match and match_phrase helper functions to gracefully handle
the case where a variable has no value, which internally is an empty
string.

This requires doing an explicit comparison to ensure that optional
parameters are not set unless the variable is actually defined.
  • Loading branch information
orangejulius committed Oct 3, 2019
1 parent 4d0298f commit dc8ac05
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/leaf/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function( property, value, params) {
};

optional_params.forEach(function(param) {
if (params && params[param]) {
if (params && params[param] && params[param].toString() !== '') {
query.match[property][param] = params[param];
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/leaf/match_phrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function( property, value, params ) {
const optional_params = ['boost', 'slop', 'analyzer'];

optional_params.forEach(function(param) {
if (params && params[param]) {
if (params && params[param] && params[param].toString() !== '') {
query.match_phrase[property][param] = params[param];
}
});
Expand Down
31 changes: 31 additions & 0 deletions test/lib/leaf/match.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const match = require('../../../lib/leaf/match');
const Variable = require('../../../lib/Variable');

module.exports.tests = {};

Expand Down Expand Up @@ -127,6 +128,36 @@ module.exports.tests.match = function(test, common) {
t.deepEqual(query, expected, 'valid match query with analyzer');
t.end();
});

test('match query does not allow empty string as optional param value', function(t) {
const query = match('property', 'value', { analyzer: ''});

const expected = {
match: {
property: {
query: 'value'
}
}
};

t.deepEqual(query, expected, 'valid match query with out empty string value');
t.end();
});

test('match query does not allow empty Variable as optional param value', function(t) {
const query = match('property', 'value', { analyzer: new Variable() });

const expected = {
match: {
property: {
query: 'value'
}
}
};

t.deepEqual(query, expected, 'valid match query with out empty string value');
t.end();
});
};

module.exports.all = function (tape, common) {
Expand Down
31 changes: 31 additions & 0 deletions test/lib/leaf/match_phrase.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const match_phrase = require('../../../lib/leaf/match_phrase');
const Variable = require('../../../lib/Variable');

module.exports.tests = {};

Expand Down Expand Up @@ -79,6 +80,36 @@ module.exports.tests.match_phrase = function(test, common) {
t.deepEqual(query, expected, 'valid match_phrase query with analyzer');
t.end();
});

test('match_phrase query does not allow empty string as optional param value', function(t) {
const query = match_phrase('property', 'value', { slop: '' });

const expected = {
match_phrase: {
property: {
query: 'value'
}
}
};

t.deepEqual(query, expected, 'valid match_phrase query without empty string value');
t.end();
});

test('match_phrase query does not allow empty Variable as optional param value', function(t) {
const query = match_phrase('property', 'value', { analyzer: new Variable() });

const expected = {
match_phrase: {
property: {
query: 'value'
}
}
};

t.deepEqual(query, expected, 'valid match_phrase query with out empty string value');
t.end();
});
};

module.exports.all = function (tape, common) {
Expand Down

0 comments on commit dc8ac05

Please sign in to comment.