Browse Source

implemented Diana's suggestions to use check-types and delete key rather than set to undefined

pull/253/head
Stephen Hess 9 years ago
parent
commit
ba22912507
  1. 14
      sanitiser/_boundary_country.js
  2. 2
      test/unit/sanitiser/reverse.js

14
sanitiser/_boundary_country.js

@ -1,4 +1,4 @@
var isObject = require('is-object'); var check = require('check-types');
var iso3166 = require('iso3166-1'); var iso3166 = require('iso3166-1');
function sanitize(raw, clean) { function sanitize(raw, clean) {
@ -8,23 +8,25 @@ function sanitize(raw, clean) {
// init clean.boundary (if not already init) // init clean.boundary (if not already init)
clean.boundary = clean.boundary || {}; clean.boundary = clean.boundary || {};
if (raw['boundary.country']) { if (check.assigned(raw['boundary.country'])) {
var country = raw['boundary.country']; var country = raw['boundary.country'];
if (typeof country !== 'string') { if (!check.string(country)) {
messages.warnings.push('boundary.country is not a string'); messages.warnings.push('boundary.country is not a string');
clean.boundary.country = undefined; delete clean.boundary.country;
} }
else if (!containsIsoCode(country.toUpperCase())) { else if (!containsIsoCode(country.toUpperCase())) {
messages.warnings.push(country + ' is not a valid ISO2/ISO3 country code'); messages.warnings.push(country + ' is not a valid ISO2/ISO3 country code');
clean.boundary.country = undefined; delete clean.boundary.country;
} }
else { else {
// the only way for boundary.country to be assigned is if input is
// a string and a known ISO2 or ISO3
clean.boundary.country = iso3166.to3(country.toUpperCase()); clean.boundary.country = iso3166.to3(country.toUpperCase());
} }
} else { } else {
clean.boundary.country = undefined; delete clean.boundary.country;
} }
return messages; return messages;

2
test/unit/sanitiser/reverse.js

@ -10,7 +10,7 @@ var reverse = require('../../../sanitiser/reverse'),
size: 10, size: 10,
details: true, details: true,
categories: [], categories: [],
boundary: { country: undefined } boundary: { }
}, },
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; sanitize = function(query, cb) { _sanitize({'query':query}, cb); };

Loading…
Cancel
Save