Browse Source

Merge pull request #1211 from pelias/trim-text

fix(sanitizer): Trim whitespace in addressit queries
pull/1212/head
Julian Simioni 6 years ago committed by GitHub
parent
commit
06ba3a79e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      sanitizer/_text_addressit.js
  2. 14
      test/unit/sanitizer/_text_addressit.js

8
sanitizer/_text_addressit.js

@ -3,6 +3,9 @@ var parser = require('addressit');
var _ = require('lodash');
var logger = require('pelias-logger').get('api');
// ref: https://en.wikipedia.org/wiki/Quotation_mark
const QUOTES = `"'«»‘’‚‛“”„‟‹›⹂「」『』〝〞〟﹁﹂﹃﹄"'「」`;
// validate texts, convert types and apply defaults
function _sanitize( raw, clean ){
@ -10,7 +13,8 @@ function _sanitize( raw, clean ){
var messages = { errors: [], warnings: [] };
// invalid input 'text'
if( !check.nonEmptyString( raw.text ) ){
const text = _.trim( _.trim( raw.text ), QUOTES );
if( !check.nonEmptyString( text ) ){
messages.errors.push('invalid param \'text\': text length, must be >0');
}
@ -18,7 +22,7 @@ function _sanitize( raw, clean ){
else {
// valid text
clean.text = raw.text;
clean.text = text;
clean.parser = 'addressit';
// remove anything that may have been parsed before

14
test/unit/sanitizer/_text_addressit.js

@ -339,6 +339,20 @@ module.exports.tests.text_parser = function(test, common) {
});
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();
});
test('return an array of expected parameters in object form for validation', (t) => {
const expected = [{ name: 'text' }];
const validParameters = sanitizer.expected();

Loading…
Cancel
Save