diff --git a/src/__tests__/NopeString.spec.ts b/src/__tests__/NopeString.spec.ts index 774c4e6..e1c65e1 100644 --- a/src/__tests__/NopeString.spec.ts +++ b/src/__tests__/NopeString.spec.ts @@ -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', () => { @@ -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); + } }); }); diff --git a/src/consts.ts b/src/consts.ts index 41bcd5c..51f5341 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -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;