From 26a6ce8d7f5117a76dcdc8bc95b1cf20ed6344dd Mon Sep 17 00:00:00 2001 From: James Buchan Date: Tue, 7 Jul 2015 13:58:37 -0600 Subject: [PATCH] add new isHttpUrl validator --- lib/valve.js | 22 +++++++++++++++++++++ package.json | 2 +- tests/test-valve.js | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/lib/valve.js b/lib/valve.js index 44a1613..c8624c8 100644 --- a/lib/valve.js +++ b/lib/valve.js @@ -794,6 +794,28 @@ Chain.prototype.isUrl = function() { }; +/** + * Adds a validator to the chain to ensure that the validated data is a + * valid-looking URL. + * + * @return {Chain} The validator chain to which the validator was added. + */ +Chain.prototype.isHttpUrl = function() { + this._pushValidator({ + name: 'isUrl', + func: function(value, baton, callback) { + if (!check.isURL(value, { protocols: ['http', 'https'], require_protocol: true })) { + callback('Invalid URL'); + return; + } + callback(null, value); + }, + help: 'URL' + }); + return this; +}; + + /* * Adds a validator to the chain to ensure that the validated data is a * valid-looking IPv4 or IPv6 address. diff --git a/package.json b/package.json index fb081ae..c029dc8 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "dependencies": { "async": "0.1.18", - "validator": "3.19.0", + "validator": "3.41.1", "ipv6": "2.0.2", "elementtree": "0.1.6", "sprintf": "0.1.1" diff --git a/tests/test-valve.js b/tests/test-valve.js index 97c72b1..c818fd5 100644 --- a/tests/test-valve.js +++ b/tests/test-valve.js @@ -396,6 +396,20 @@ exports['test_validate_url'] = function(test, assert) { assert.deepEqual(cleaned, obj, 'URL test'); }); + // ftp test + var ftp_url = { a: "ftp://rackspace.com" }; + v.check(ftp_url, function(err, cleaned) { + assert.ifError(err); + assert.deepEqual(cleaned, ftp_url, 'FTP URL test'); + }); + + // no protocol test + var no_proto = { a: "rackspace.com" }; + v.check(no_proto, function(err, cleaned) { + assert.ifError(err); + assert.deepEqual(cleaned, no_proto, "URL test with no protocol specified"); + }); + // negative case var neg = { a: 'invalid/' }; v.check(neg, function(err, cleaned) { @@ -404,6 +418,39 @@ exports['test_validate_url'] = function(test, assert) { test.finish(); }; +exports['test_validate_http_url'] = function(test, assert) { + var v = new V({ + a: C().isHttpUrl() + }); + + // positive case + var obj = { a: 'http://www.cloudkick.com' }; + var obj_ext = { a: 'http://www.cloudkick.com', b: 2 }; + v.check(obj_ext, function(err, cleaned) { + assert.ifError(err); + assert.deepEqual(cleaned, obj, 'URL test'); + }); + + // ftp test + var ftp_url = { a: 'ftp://rackspace.com' }; + v.check(ftp_url, function(err, cleaned) { + assert.deepEqual(err.message, 'Invalid URL', 'HTTP URL test with FTP URL'); + }); + + // no protocol test + var no_proto = { a: 'rackspace.com' }; + v.check(no_proto, function(err, cleaned) { + assert.deepEqual(err.message, 'Invalid URL', 'HTTP URL test with no protocol specified'); + }); + + // negative case + var neg = { a: 'invalid/' }; + v.check(neg, function(err, cleaned) { + assert.deepEqual(err.message, 'Invalid URL', 'HTTP URL test (negative case)'); + }); + test.finish(); +}; + exports['test_validate_ipv6'] = function(test, assert) { var v = new V({ a: C().isIPv6()