From 07c637f5a6e6b3a3ee62dfc3b176ffee8e92d9f6 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Fri, 3 Jun 2016 15:28:58 -0400 Subject: [PATCH] Include a new pelias-categories module for category validation --- package.json | 1 + sanitiser/_categories.js | 7 +++++-- test/unit/sanitiser/_categories.js | 27 +++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 4fa1488e..53559104 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "pelias-query": "8.3.0", "pelias-text-analyzer": "1.3.0", "stats-lite": "2.0.3", + "pelias-categories": "1.0.0", "through2": "2.0.1" }, "devDependencies": { diff --git a/sanitiser/_categories.js b/sanitiser/_categories.js index 63d6324e..06e65800 100644 --- a/sanitiser/_categories.js +++ b/sanitiser/_categories.js @@ -1,5 +1,6 @@ var check = require('check-types'); +var categoryTaxonomy = require('pelias-categories'); var ERRORS = { 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 -function sanitize( raw, clean, validCategories ) { +function sanitize( raw, clean, categories ) { + + categories = categories || categoryTaxonomy; // error & warning messages var messages = {errors: [], warnings: []}; @@ -30,7 +33,7 @@ function sanitize( raw, clean, validCategories ) { return cat.toLowerCase().trim(); // lowercase inputs }) .filter(function (cat) { - if (check.nonEmptyString(cat) && validCategories && validCategories.indexOf(cat) !== -1) { + if (check.nonEmptyString(cat) && categories.isValidCategory(cat)) { return true; } throw new Error('Empty string value'); diff --git a/test/unit/sanitiser/_categories.js b/test/unit/sanitiser/_categories.js index b896915d..743423a9 100644 --- a/test/unit/sanitiser/_categories.js +++ b/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) { - 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) { + isValidCategoryCalled = 0; + var req = { query: { categories: 'food' @@ -73,29 +80,41 @@ module.exports.tests.valid_categories = function(test, common) { t.deepEqual(messages.errors, [], 'no error returned'); t.deepEqual(messages.warnings, [], 'no warnings returned'); + t.equal(isValidCategoryCalled, 1); + t.end(); }); test('multiple categories', function(t) { + isValidCategoryCalled = 0; var req = { query: { categories: 'food,health' }, clean: { } }; + var expectedCategories = ['food', 'health']; 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'); t.deepEqual(messages.errors, [], 'no error returned'); t.deepEqual(messages.warnings, [], 'no warnings returned'); + + t.equal(isValidCategoryCalled, expectedCategories.length); + t.end(); }); }; 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) { var req = { @@ -132,7 +151,7 @@ module.exports.tests.invalid_categories = function(test, common) { 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.equal(req.clean.categories, undefined, 'clean.categories should remain empty');