You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB

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.',
invalid: 'Invalid categories parameter value(s). See documentation of service for valid options.'
};
// validate inputs, convert types and apply defaults
function sanitize( raw, clean, categories ) {
categories = categories || categoryTaxonomy;
// error & warning messages
var messages = {errors: [], warnings: []};
// it's not a required parameter, so if it's not provided just move on
if (!raw.hasOwnProperty('categories')) {
return messages;
}
if (!check.nonEmptyString(raw.categories)) {
messages.errors.push(ERRORS.empty);
return messages;
}
// if categories string has been set
// map input categories to valid format
try {
clean.categories = raw.categories.split(',')
.map(function (cat) {
return cat.toLowerCase().trim(); // lowercase inputs
})
.filter(function (cat) {
if (check.nonEmptyString(cat) && categories.isValidCategory(cat)) {
return true;
}
throw new Error('Empty string value');
});
} catch (err) {
// remove everything from the list if there was any error
delete clean.categories;
}
if (check.undefined(clean.categories) || check.emptyArray(clean.categories)) {
messages.errors.push(ERRORS.invalid);
}
return messages;
}
// export function
module.exports = sanitize;