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.
34 lines
844 B
34 lines
844 B
9 years ago
|
var check = require('check-types'),
|
||
8 years ago
|
text_analyzer = require('pelias-text-analyzer');
|
||
10 years ago
|
|
||
9 years ago
|
// validate texts, convert types and apply defaults
|
||
9 years ago
|
function sanitize( raw, clean ){
|
||
9 years ago
|
|
||
9 years ago
|
// error & warning messages
|
||
|
var messages = { errors: [], warnings: [] };
|
||
10 years ago
|
|
||
9 years ago
|
// invalid input 'text'
|
||
8 years ago
|
// must call `!check.nonEmptyString` since `check.emptyString` returns
|
||
|
// `false` for `undefined` and `null`
|
||
9 years ago
|
if( !check.nonEmptyString( raw.text ) ){
|
||
9 years ago
|
messages.errors.push('invalid param \'text\': text length, must be >0');
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// valid input 'text'
|
||
|
else {
|
||
|
// valid text
|
||
9 years ago
|
clean.text = raw.text;
|
||
9 years ago
|
|
||
9 years ago
|
// parse text with query parser
|
||
9 years ago
|
var parsed_text = text_analyzer.parse(clean.text);
|
||
9 years ago
|
if (check.assigned(parsed_text)) {
|
||
|
clean.parsed_text = parsed_text;
|
||
|
}
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
return messages;
|
||
10 years ago
|
}
|
||
|
|
||
|
// export function
|
||
9 years ago
|
module.exports = sanitize;
|