Skip to content

Commit c574ed3

Browse files
author
Mark
committed
url regex fix
1 parent cf555ae commit c574ed3

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/Raw/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const _ = require('lodash');
66
/**
77
* list of creepy regex, no they work nice
88
*/
9-
const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,63}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/i;
9+
const urlRegex = /https?:\/\/(www\.)?([-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-z]{2,63}|localhost)\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/i;
1010
const emailRegex = /^([\w-]+(?:\.[\w-]+)*)(\+[\w\.-]+)?@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,63}(?:\.[a-z]{2})?)$/i;
1111
const phoneRegex = /\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/;
1212
const creditCardRegex = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})$/;

test/raw.spec.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Raw Validator', function () {
1212
});
1313

1414
it('should return false when input is an object', function () {
15-
const isArray = Is.array({age: 22});
15+
const isArray = Is.array({ age: 22 });
1616
expect(isArray).to.equal(false);
1717
});
1818

@@ -108,22 +108,22 @@ describe('Raw Validator', function () {
108108
});
109109

110110
it('should return true when input is an object', function () {
111-
const isObject = Is.object({name: 'virk'});
111+
const isObject = Is.object({ name: 'virk' });
112112
expect(isObject).to.equal(true);
113113
});
114114

115115
it('should return false when input is not a stringify object', function () {
116-
const isObject = Is.object(JSON.stringify({name: 'virk'}));
116+
const isObject = Is.object(JSON.stringify({ name: 'virk' }));
117117
expect(isObject).to.equal(false);
118118
});
119119

120120
it('should return true when input is json', function () {
121-
const isJson = Is.json(JSON.stringify({name: 'virk'}));
121+
const isJson = Is.json(JSON.stringify({ name: 'virk' }));
122122
expect(isJson).to.equal(true);
123123
});
124124

125125
it('should return false when input is an object', function () {
126-
const isJson = Is.json({name: 'virk'});
126+
const isJson = Is.json({ name: 'virk' });
127127
expect(isJson).to.equal(false);
128128
});
129129

@@ -291,6 +291,21 @@ describe('Raw Validator', function () {
291291
expect(isUrl).to.equal(false);
292292
});
293293

294+
it('should return true when input contains only localhost', function () {
295+
const isUrl = Is.url('http://localhost');
296+
expect(isUrl).to.equal(true);
297+
});
298+
299+
it('should return true when input contains localhost with port', function () {
300+
const isUrl = Is.url('http://localhost:80');
301+
expect(isUrl).to.equal(true);
302+
});
303+
304+
it('should return true when domain name part contains 1 character', function () {
305+
const isUrl = Is.url('https://t.co');
306+
expect(isUrl).to.equal(true);
307+
});
308+
294309
it('should return false when input is not a valid email', function () {
295310
const isEmail = Is.email('mail');
296311
expect(isEmail).to.equal(false);

0 commit comments

Comments
 (0)