Browse Source

Merge pull request #1171 from pelias/text-trim-whitespace

Trim whitespace and quotes before checking text length
pull/1175/head
Julian Simioni 6 years ago committed by GitHub
parent
commit
6bff132e69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      sanitizer/_text.js
  2. 14
      test/unit/sanitizer/_text.js

7
sanitizer/_text.js

@ -13,11 +13,12 @@ function _sanitize( raw, clean ){
// invalid input 'text'
// must call `!check.nonEmptyString` since `check.emptyString` returns
// `false` for `undefined` and `null`
if( !check.nonEmptyString( raw.text ) ){
messages.errors.push('invalid param \'text\': text length, must be >0');
const text = _.trim( _.trim( raw.text ), QUOTES );
if( !check.nonEmptyString( text ) ){
messages.errors.push('invalid param \'text\': text length, must be >0');
} else {
clean.text = _.trim( _.trim( raw.text ), QUOTES );
clean.text = text;
}
return messages;

14
test/unit/sanitizer/_text.js

@ -124,6 +124,20 @@ module.exports.tests.text_parser = function(test, common) {
t.deepEquals(validParameters, expected);
t.end();
});
test('whitespace-only input counts as empty', (t) => {
const raw = { text: ' ' };
const clean = {};
const expected_clean = {};
const messages = sanitizer.sanitize(raw, clean);
t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, ['invalid param \'text\': text length, must be >0']);
t.deepEquals(messages.warnings, [], 'no warnings');
t.end();
});
};
module.exports.all = (tape, common) => {

Loading…
Cancel
Save