Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[do not merge] do not throw on name/address regex matching errors #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Document.prototype.setName = function( prop, value ){

validate.type('string', value);
validate.truthy(value);
validate.regex.nomatch(value, /https?:\/\//);
validate.regex.nomatch(value, /https?:\/\//, { throw: false });

// must copy name to 'phrase' index
if( Array.isArray( this.name[ prop ] ) ){
Expand All @@ -263,7 +263,7 @@ Document.prototype.setNameAlias = function( prop, value ){

validate.type('string', value);
validate.truthy(value);
validate.regex.nomatch(value, /https?:\/\//);
validate.regex.nomatch(value, /https?:\/\//, { throw: false });

// is this the first time setting this prop? ensure it's an array
if( !this.hasName( prop ) ){
Expand Down Expand Up @@ -405,7 +405,7 @@ Document.prototype.setAddress = function( prop, value ){
validate.type('string', value);
validate.truthy(value);
validate.property(addressFields, prop);
validate.regex.nomatch(value, /https?:\/\//);
validate.regex.nomatch(value, /https?:\/\//, { throw: false });

if( Array.isArray( this.address_parts[ prop ] ) ){
this.address_parts[ prop ][ 0 ] = value;
Expand All @@ -421,7 +421,7 @@ Document.prototype.setAddressAlias = function( prop, value ){
validate.type('string', value);
validate.truthy(value);
validate.property(addressFields, prop);
validate.regex.nomatch(value, /https?:\/\//);
validate.regex.nomatch(value, /https?:\/\//, { throw: false });

// is this the first time setting this prop? ensure it's an array
if( !this.hasAddress( prop ) ){
Expand Down
20 changes: 14 additions & 6 deletions test/document/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ module.exports.tests.setAddress = function(test) {
t.equal(doc.getAddress('test'), undefined, 'property not set');
t.end();
});
test('setAddress - http regex', function (t) {
test('setAddress - http regex - throw false', function (t) {
var doc = new Document('mysource', 'mylayer', 'myid');
t.throws(doc.setAddress.bind(doc, 'number', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.throws(doc.setAddress.bind(doc, 'number', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setAddress.bind(doc, 'number', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setAddress.bind(doc, 'number', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.end();
});
};
Expand Down Expand Up @@ -114,10 +114,18 @@ module.exports.tests.setAddressAlias = function(test) {
t.deepEqual(doc.getAddressAliases('test'), [], 'property not set');
t.end();
});
test('setAddressAlias - http regex', function (t) {
test('setAddressAlias - http regex - throw false', function (t) {
var doc = new Document('mysource', 'mylayer', 'myid');
t.throws(doc.setAddressAlias.bind(doc, 'number', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.throws(doc.setAddressAlias.bind(doc, 'number', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.doesNotThrow(
doc.setAddressAlias.bind(doc, 'number', 'http://www.pelias.io'),
/invalid regex/,
'regex failure'
);
t.doesNotThrow(
doc.setAddressAlias.bind(doc, 'number', 'AAhttp://www.pelias.ioBB'),
/invalid regex/,
'regex failure'
);
t.end();
});
};
Expand Down
12 changes: 6 additions & 6 deletions test/document/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ module.exports.tests.setName = function(test) {
t.equal(doc.getName('test'), undefined, 'property not set');
t.end();
});
test('setName - http regex', function (t) {
test('setName - http regex - throw false', function (t) {
var doc = new Document('mysource', 'mylayer', 'myid');
t.throws(doc.setName.bind(doc, 'default', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.throws(doc.setName.bind(doc, 'default', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setName.bind(doc, 'default', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setName.bind(doc, 'default', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.end();
});
};
Expand Down Expand Up @@ -111,10 +111,10 @@ module.exports.tests.setNameAlias = function(test) {
t.deepEqual(doc.getNameAliases('test'), [], 'property not set');
t.end();
});
test('setNameAlias - http regex', function (t) {
test('setNameAlias - http regex - throw false', function (t) {
var doc = new Document('mysource', 'mylayer', 'myid');
t.throws(doc.setNameAlias.bind(doc, 'default', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.throws(doc.setNameAlias.bind(doc, 'default', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setNameAlias.bind(doc, 'default', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setNameAlias.bind(doc, 'default', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.end();
});
};
Expand Down
6 changes: 6 additions & 0 deletions test/util/valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports.tests.regex = (test) => {
t.doesNotThrow(valid.regex.nomatch.bind(null, 'hello', /bye/), /invalid regex/);
t.end();
});

test('regex nomatch - throw false', (t) => {
const options = { throw: false };
t.doesNotThrow(valid.regex.nomatch.bind(null, 'hello', /he/, options), /invalid regex/);
t.end();
});
};

module.exports.all = (tape, common) => {
Expand Down
13 changes: 9 additions & 4 deletions util/valid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var _ = require('lodash'),
PeliasModelError = require('../errors').PeliasModelError;
const _ = require('lodash');
const PeliasModelError = require('../errors').PeliasModelError;

module.exports.type = function( type, val ){
if( type.toLowerCase() === 'array' ){
Expand Down Expand Up @@ -112,9 +112,14 @@ module.exports.boundingBox = function( val ) {
};

module.exports.regex = {
nomatch: function(val, regex) {
nomatch: function(val, regex, options) {
if( regex.test(val) ){
throw new PeliasModelError(`invalid regex test, ${val} should not match ${regex}`);
const message = `invalid regex test, ${val} should not match ${regex}`;
if( _.get(options, 'throw', true) === false ){
console.warn(message);
} else {
throw new PeliasModelError(message);
}
}

return module.exports;
Expand Down