Browse Source

Include a new pelias-categories module for category validation

pull/546/head
Diana Shkolnikov 9 years ago
parent
commit
07c637f5a6
  1. 1
      package.json
  2. 7
      sanitiser/_categories.js
  3. 27
      test/unit/sanitiser/_categories.js

1
package.json

@ -56,6 +56,7 @@
"pelias-query": "8.3.0", "pelias-query": "8.3.0",
"pelias-text-analyzer": "1.3.0", "pelias-text-analyzer": "1.3.0",
"stats-lite": "2.0.3", "stats-lite": "2.0.3",
"pelias-categories": "1.0.0",
"through2": "2.0.1" "through2": "2.0.1"
}, },
"devDependencies": { "devDependencies": {

7
sanitiser/_categories.js

@ -1,5 +1,6 @@
var check = require('check-types'); var check = require('check-types');
var categoryTaxonomy = require('pelias-categories');
var ERRORS = { var ERRORS = {
empty: 'Categories parameter cannot be left blank. See documentation of service for valid options.', empty: 'Categories parameter cannot be left blank. See documentation of service for valid options.',
@ -7,7 +8,9 @@ var ERRORS = {
}; };
// validate inputs, convert types and apply defaults // validate inputs, convert types and apply defaults
function sanitize( raw, clean, validCategories ) { function sanitize( raw, clean, categories ) {
categories = categories || categoryTaxonomy;
// error & warning messages // error & warning messages
var messages = {errors: [], warnings: []}; var messages = {errors: [], warnings: []};
@ -30,7 +33,7 @@ function sanitize( raw, clean, validCategories ) {
return cat.toLowerCase().trim(); // lowercase inputs return cat.toLowerCase().trim(); // lowercase inputs
}) })
.filter(function (cat) { .filter(function (cat) {
if (check.nonEmptyString(cat) && validCategories && validCategories.indexOf(cat) !== -1) { if (check.nonEmptyString(cat) && categories.isValidCategory(cat)) {
return true; return true;
} }
throw new Error('Empty string value'); throw new Error('Empty string value');

27
test/unit/sanitiser/_categories.js

@ -57,9 +57,16 @@ module.exports.tests.no_categories = function(test, common) {
}; };
module.exports.tests.valid_categories = function(test, common) { module.exports.tests.valid_categories = function(test, common) {
var validCategories = ['food','health','financial','education','government']; var isValidCategoryCalled = 0;
var validCategories = {
isValidCategory: function (cat) {
isValidCategoryCalled++;
return ['food','health','financial','education','government'].indexOf(cat) !== -1; }
};
test('single category', function(t) { test('single category', function(t) {
isValidCategoryCalled = 0;
var req = { var req = {
query: { query: {
categories: 'food' categories: 'food'
@ -73,29 +80,41 @@ module.exports.tests.valid_categories = function(test, common) {
t.deepEqual(messages.errors, [], 'no error returned'); t.deepEqual(messages.errors, [], 'no error returned');
t.deepEqual(messages.warnings, [], 'no warnings returned'); t.deepEqual(messages.warnings, [], 'no warnings returned');
t.equal(isValidCategoryCalled, 1);
t.end(); t.end();
}); });
test('multiple categories', function(t) { test('multiple categories', function(t) {
isValidCategoryCalled = 0;
var req = { var req = {
query: { query: {
categories: 'food,health' categories: 'food,health'
}, },
clean: { } clean: { }
}; };
var expectedCategories = ['food', 'health'];
var messages = sanitize(req.query, req.clean, validCategories); var messages = sanitize(req.query, req.clean, validCategories);
t.deepEqual(req.clean.categories, ['food', 'health'], t.deepEqual(req.clean.categories, expectedCategories,
'clean.categories should be an array with proper values'); 'clean.categories should be an array with proper values');
t.deepEqual(messages.errors, [], 'no error returned'); t.deepEqual(messages.errors, [], 'no error returned');
t.deepEqual(messages.warnings, [], 'no warnings returned'); t.deepEqual(messages.warnings, [], 'no warnings returned');
t.equal(isValidCategoryCalled, expectedCategories.length);
t.end(); t.end();
}); });
}; };
module.exports.tests.invalid_categories = function(test, common) { module.exports.tests.invalid_categories = function(test, common) {
var validCategories = ['food','health','financial','education','government']; var isValidCategoryCalled = 0;
var validCategories = {
isValidCategory: function (cat) {
isValidCategoryCalled++;
return ['food','health','financial','education','government'].indexOf(cat) !== -1; }
};
test('garbage category', function(t) { test('garbage category', function(t) {
var req = { var req = {
@ -132,7 +151,7 @@ module.exports.tests.invalid_categories = function(test, common) {
warnings: [] warnings: []
}; };
var messages = sanitize(req.query, req.clean); var messages = sanitize(req.query, req.clean, validCategories);
t.deepEqual(messages, expected_messages, 'error with message returned'); t.deepEqual(messages, expected_messages, 'error with message returned');
t.equal(req.clean.categories, undefined, 'clean.categories should remain empty'); t.equal(req.clean.categories, undefined, 'clean.categories should remain empty');

Loading…
Cancel
Save