Skip to content

Commit

Permalink
url now matches null and blank strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrelogan committed Aug 1, 2016
1 parent 2b8d443 commit e45b08b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions index.es6
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export let library = {
return library.required(value) && library.exists(searchString) && value.trim().substr(0, searchString.length) === searchString;
},
url: function(value) {
// matches blank strings so you have a choice of pairing it with required
if (value === null || (typeof(value) === 'string' && value.trim() === '')) {
return true;
}
//
// https://gist.github.com/dperini/729294
//
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ var library = exports.library = {
return library.required(value) && library.exists(searchString) && value.trim().substr(0, searchString.length) === searchString;
},
url: function url(value) {
// matches blank strings so you have a choice of pairing it with required
if (value === null || typeof value === 'string' && value.trim() === '') {
return true;
}
//
// https://gist.github.com/dperini/729294
//
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-formstate-validation",
"version": "0.1.5",
"version": "0.2.0",
"peerDependencies": {
"react-formstate": ">=0.3.0"
},
Expand Down
8 changes: 6 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ describe('validation library', function() {
it('does not match schema relative', function() { assert.equal(false, lib.url('//react-formstate-validation.test')); });
it('does not match site relative', function() { assert.equal(false, lib.url('~/react-formstate-validation.test')); });
it('does not match gopher', function() { assert.equal(false, lib.url('gopher://react-formstate-validation.test')); });
it('does not crash', function() { assert.equal(false, lib.url(null)); });
it('matches null', function() { assert.equal(true, lib.url(null)); });
it('matches empty string', function() { assert.equal(true, lib.url('')); });
it('matches blank string', function() { assert.equal(true, lib.url(' ')); });
it('does not crash on an integer', function() { assert.equal(false, lib.url(3)); });
it('does not crash on an object', function() { assert.equal(false, lib.url({x: 3})); });
});
});

Expand Down Expand Up @@ -337,7 +341,7 @@ describe('Messages', function() {
});
describe('#url', function() {
it('has a message', function() {
assert.equal('Field must be a url', v['url']('','Field'));
assert.equal('Field must be a url', v['url']('badUrl','Field'));
});
it('might not return a message', function() {
assert.equal(undefined, v['url']('http://test.test','Field'));
Expand Down

0 comments on commit e45b08b

Please sign in to comment.