Skip to content

Commit

Permalink
fix url regexp (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvego committed Apr 9, 2021
1 parent 4564b74 commit c8af9f9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
35 changes: 21 additions & 14 deletions src/__tests__/NopeString.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ describe('#NopeString', () => {
});

it('should return an error message for an invalid entry', () => {
expect(Nope.string().regex(/abc/i, 'errorMessage').validate('http:google.com')).toBe(
'errorMessage',
);
expect(Nope.string().regex(/abc/i, 'errorMessage').validate('defg')).toBe('errorMessage');
});

it('should return undefined for an valid entry', () => {
Expand All @@ -23,20 +21,29 @@ describe('#NopeString', () => {
});

it('should return an error message for an invalid URL', () => {
expect(Nope.string().url().validate('http:google.com')).toBe('Input is not a valid url');
const invalidUrls = [
'http://:google.com',
'http://',
// 'http:///a',
'http://foo.bar/foo(bar)baz quux',
];
for (const url of invalidUrls) {
expect(Nope.string().url().validate(url)).toBe('Input is not a valid url');
}
});

it('should return undefined for an valid URL', () => {
expect(Nope.string().url('urlErrorMessage').validate('https://google.com')).toBe(undefined);
expect(Nope.string().url('urlErrorMessage').validate('https://google.com?asd=123')).toBe(
undefined,
);
expect(Nope.string().url('urlErrorMessage').validate('https://google.com/123')).toBe(
undefined,
);
expect(Nope.string().url('urlErrorMessage').validate('https://google.com/123/456?q=42')).toBe(
undefined,
);
const validUrls = [
'https://github.com/bvego/nope-validator/commit/4564b7444dcd92769e5c5b80420469c9f18b7a05?branch=4564b7444dcd92769e5c5b80420469c9f18b7a05&diff=split',
'https://google.com',
'https://google.com?asd=123',
'https://google.com/123',
'https://google.com/123/456?q=42',
];

for (const url of validUrls) {
expect(Nope.string().url('urlErrorMessage').validate(url)).toBe(undefined);
}
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

export const urlRegex = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i;
export const urlRegex = /^([a-z][a-z0-9\*\-\.]*):\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*(?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@)?(?:(?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?](?:[\w#!:\.\?\+=&@!$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/i;

0 comments on commit c8af9f9

Please sign in to comment.