Skip to content

Commit 4a69858

Browse files
committed
feat(places-category): Add warning message for non-empty categories
1 parent f9ae79d commit 4a69858

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

sanitizer/_categories.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const _ = require('lodash');
22
const categoryTaxonomy = require('pelias-categories');
33

44
const WARNINGS = {
5-
empty: 'Categories parameter left blank, showing results from all categories.'
5+
empty: 'Categories parameter left blank, showing results from all categories.',
6+
notEmpty: 'Categories filtering not supported on this endpoint, showing results from all categories.'
67
};
78

89
// validate inputs, convert types and apply defaults
@@ -39,11 +40,17 @@ function _sanitize (raw, clean, categories) {
3940
}
4041

4142
function _alwaysBlank (raw, clean, categories) {
43+
// error & warning messages
44+
const messages = { errors: [], warnings: [] };
45+
4246
if (raw.hasOwnProperty('categories')) {
4347
clean.categories = [];
48+
if (_.isString(raw.categories) && !_.isEmpty(raw.categories)) {
49+
messages.warnings.push(WARNINGS.notEmpty);
50+
}
4451
}
4552

46-
return { errors: [], warnings: [] };
53+
return messages;
4754
}
4855

4956
function _expected () {

test/unit/sanitizer/_categories.js

+45-9
Original file line numberDiff line numberDiff line change
@@ -186,45 +186,81 @@ module.exports.tests.invalid_categories = function(test, common) {
186186
module.exports.tests.always_blank = function(test, common) {
187187
const alwaysBlankSanitizer = require( '../../../sanitizer/_categories')(true);
188188
test('garbage category', function(t) {
189-
var req = {
189+
const req = {
190190
query: {
191191
categories: 'barf'
192192
},
193193
clean: { }
194194
};
195-
var expected_messages = { errors: [], warnings: [] };
195+
const expected_messages = { errors: [], warnings: [
196+
'Categories filtering not supported on this endpoint, showing results from all categories.'
197+
] };
196198

197-
var messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);
199+
const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);
198200

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

204206
test('all garbage categories', function(t) {
205-
var req = {
207+
const req = {
206208
query: {
207209
categories: 'food'
208210
},
209211
clean: { }
210212
};
211-
var expected_messages = { errors: [], warnings: [] };
213+
const expected_messages = { errors: [], warnings: [
214+
'Categories filtering not supported on this endpoint, showing results from all categories.'
215+
] };
216+
217+
const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);
218+
219+
t.deepEqual(messages, expected_messages, 'error with message returned');
220+
t.deepEqual(req.clean.categories, [], 'should return empty array');
221+
t.end();
222+
});
223+
224+
test('defined categories', function(t) {
225+
const req = {
226+
query: {
227+
categories: undefined
228+
},
229+
clean: { }
230+
};
231+
const expected_messages = { errors: [], warnings: [] };
212232

213-
var messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);
233+
const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);
234+
235+
t.deepEqual(messages, expected_messages, 'error with message returned');
236+
t.deepEqual(req.clean.categories, [], 'should return empty array');
237+
t.end();
238+
});
239+
240+
test('empty categories', function(t) {
241+
const req = {
242+
query: {
243+
categories: ''
244+
},
245+
clean: { }
246+
};
247+
const expected_messages = { errors: [], warnings: [] };
248+
249+
const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);
214250

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

220256
test('not defined categories', function(t) {
221-
var req = {
257+
const req = {
222258
query: { },
223259
clean: { }
224260
};
225-
var expected_messages = { errors: [], warnings: [] };
261+
const expected_messages = { errors: [], warnings: [] };
226262

227-
var messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);
263+
const messages = alwaysBlankSanitizer.sanitize(req.query, req.clean);
228264

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

0 commit comments

Comments
 (0)