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