Skip to content

Commit

Permalink
feat(places-category): add categories to the places endpoint
Browse files Browse the repository at this point in the history
Categories are something really useful (and interesting) that's why they should also be available in the Places API.
This PR will add categories when we use the endpoint places e.g `/v1/places?categories&ids=...`
  • Loading branch information
Joxit committed Nov 27, 2019
1 parent 5632edf commit 9a8e898
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
12 changes: 10 additions & 2 deletions sanitizer/_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ function _sanitize (raw, clean, categories) {
return messages;
}

function _alwaysBlank (raw, clean, categories) {
if (raw.hasOwnProperty('categories')) {
clean.categories = [];
}

return { errors: [], warnings: [] };
}

function _expected () {
return [{ name: 'categories' }];
}
// export function
module.exports = () => ({
sanitize: _sanitize,
module.exports = (alwaysBlank) => ({
sanitize: alwaysBlank ? _alwaysBlank : _sanitize,
expected: _expected
});
1 change: 1 addition & 0 deletions sanitizer/place.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var sanitizeAll = require('../sanitizer/sanitizeAll'),
debug: require('../sanitizer/_debug')(),
ids: require('../sanitizer/_ids')(),
private: require('../sanitizer/_flag_bool')('private', false),
categories: require('../sanitizer/_categories')(true),
request_language: require('../sanitizer/_request_language')()
};

Expand Down
49 changes: 49 additions & 0 deletions test/unit/sanitizer/_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,55 @@ module.exports.tests.invalid_categories = function(test, common) {
});
};

module.exports.tests.always_blank = function(test, common) {
const alwaysBlankSanitizer = require( '../../../sanitizer/_categories')(true);
test('garbage category', function(t) {
var req = {
query: {
categories: 'barf'
},
clean: { }
};
var expected_messages = { errors: [], warnings: [] };

var messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);

t.deepEqual(messages, expected_messages, 'error with message returned');
t.deepEqual(req.clean.categories, [], 'should return empty array');
t.end();
});

test('all garbage categories', function(t) {
var req = {
query: {
categories: 'food'
},
clean: { }
};
var expected_messages = { errors: [], warnings: [] };

var messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);

t.deepEqual(messages, expected_messages, 'error with message returned');
t.deepEqual(req.clean.categories, [], 'should return empty array');
t.end();
});

test('not defined categories', function(t) {
var req = {
query: { },
clean: { }
};
var expected_messages = { errors: [], warnings: [] };

var messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);

t.deepEqual(messages, expected_messages, 'error with message returned');
t.deepEqual(req.clean.categories, undefined, 'categories should be undefined');
t.end();
});
};

module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANITIZE _categories ' + name, testFunction);
Expand Down

0 comments on commit 9a8e898

Please sign in to comment.