Skip to content

Commit e45b08b

Browse files
committed
url now matches null and blank strings
1 parent 2b8d443 commit e45b08b

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

index.es6

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export let library = {
4848
return library.required(value) && library.exists(searchString) && value.trim().substr(0, searchString.length) === searchString;
4949
},
5050
url: function(value) {
51+
// matches blank strings so you have a choice of pairing it with required
52+
if (value === null || (typeof(value) === 'string' && value.trim() === '')) {
53+
return true;
54+
}
5155
//
5256
// https://gist.github.com/dperini/729294
5357
//

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ var library = exports.library = {
6767
return library.required(value) && library.exists(searchString) && value.trim().substr(0, searchString.length) === searchString;
6868
},
6969
url: function url(value) {
70+
// matches blank strings so you have a choice of pairing it with required
71+
if (value === null || typeof value === 'string' && value.trim() === '') {
72+
return true;
73+
}
7074
//
7175
// https://gist.github.com/dperini/729294
7276
//

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-formstate-validation",
3-
"version": "0.1.5",
3+
"version": "0.2.0",
44
"peerDependencies": {
55
"react-formstate": ">=0.3.0"
66
},

test/test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ describe('validation library', function() {
171171
it('does not match schema relative', function() { assert.equal(false, lib.url('//react-formstate-validation.test')); });
172172
it('does not match site relative', function() { assert.equal(false, lib.url('~/react-formstate-validation.test')); });
173173
it('does not match gopher', function() { assert.equal(false, lib.url('gopher://react-formstate-validation.test')); });
174-
it('does not crash', function() { assert.equal(false, lib.url(null)); });
174+
it('matches null', function() { assert.equal(true, lib.url(null)); });
175+
it('matches empty string', function() { assert.equal(true, lib.url('')); });
176+
it('matches blank string', function() { assert.equal(true, lib.url(' ')); });
177+
it('does not crash on an integer', function() { assert.equal(false, lib.url(3)); });
178+
it('does not crash on an object', function() { assert.equal(false, lib.url({x: 3})); });
175179
});
176180
});
177181

@@ -337,7 +341,7 @@ describe('Messages', function() {
337341
});
338342
describe('#url', function() {
339343
it('has a message', function() {
340-
assert.equal('Field must be a url', v['url']('','Field'));
344+
assert.equal('Field must be a url', v['url']('badUrl','Field'));
341345
});
342346
it('might not return a message', function() {
343347
assert.equal(undefined, v['url']('http://test.test','Field'));

0 commit comments

Comments
 (0)