Skip to content

Commit

Permalink
Merge pull request #48 from racker/jjbuchan/add_http_url_validation
Browse files Browse the repository at this point in the history
add new isHttpUrl validator
  • Loading branch information
jirwin committed Jul 7, 2015
2 parents 3a353fb + 26a6ce8 commit 40edbca
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/valve.js
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
Expand Down
47 changes: 47 additions & 0 deletions tests/test-valve.js
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down

0 comments on commit 40edbca

Please sign in to comment.