Browse Source

consider postalcode-only input an error condition

pull/712/head
Stephen Hess 8 years ago
parent
commit
6f04f67d9b
  1. 10
      sanitizer/_synthesize_analysis.js
  2. 22
      test/unit/sanitizer/_synthesize_analysis.js

10
sanitizer/_synthesize_analysis.js

@ -7,6 +7,11 @@ function normalizeWhitespaceToSingleSpace(val) {
return _.replace(_.trim(val), /\s+/g, ' ');
}
function isPostalCodeOnly(parsed_text) {
return Object.keys(parsed_text).length === 1 &&
parsed_text.hasOwnProperty('postalcode');
}
function sanitize( raw, clean ){
// error & warning messages
@ -22,7 +27,10 @@ function sanitize( raw, clean ){
}, {});
if (_.isEmpty(Object.keys(clean.parsed_text))) {
if (isPostalCodeOnly(clean.parsed_text)) {
messages.errors.push('postalcode-only inputs are not supported');
}
else if (_.isEmpty(Object.keys(clean.parsed_text))) {
messages.errors.push(
`at least one of the following fields is required: ${fields.join(', ')}`);
}

22
test/unit/sanitizer/_synthesize_analysis.js

@ -91,6 +91,28 @@ module.exports.tests.text_parser = function(test, common) {
});
test('postalcode-only parsed_text should return error', function(t) {
const raw = {
postalcode: 'postalcode value'
};
const clean = {};
const expected_clean = {
parsed_text: {
postalcode: 'postalcode value'
}
};
const messages = sanitizer(raw, clean);
t.deepEquals(clean, expected_clean);
t.deepEquals(messages.errors, ['postalcode-only inputs are not supported'], 'no errors');
t.deepEquals(messages.warnings, [], 'no warnings');
t.end();
});
};
module.exports.all = function (tape, common) {

Loading…
Cancel
Save