diff --git a/src/string.ts b/src/string.ts index c193d001..b4fb60e5 100644 --- a/src/string.ts +++ b/src/string.ts @@ -23,7 +23,7 @@ let rEmail = let rUrl = // eslint-disable-next-line - /^((https?|ftp):)?\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(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()*+,;=:@\/?]*)?(?:#[a-z\d\-._~%!$&'()*+,;=:@\/?]*)?$/i; // eslint-disable-next-line let rUUID = diff --git a/test/string.ts b/test/string.ts index f8c24b5c..69285abd 100644 --- a/test/string.ts +++ b/test/string.ts @@ -1,12 +1,12 @@ import * as TestHelpers from './helpers'; import { - string, + AnySchema, number, object, ref, + string, ValidationError, - AnySchema, } from '../src'; describe('String types', () => { @@ -209,7 +209,17 @@ describe('String types', () => { return Promise.all([ expect(v.isValid('//www.github.com/')).resolves.toBe(true), + expect(v.isValid('https://username:password@127.0.0.1:8080/path#fragment')).resolves.toBe(true), + expect(v.isValid('https://username:password@github.com/path#fragment')).resolves.toBe(true), + expect(v.isValid('http://127.0.0.1:8080/')).resolves.toBe(true), + expect(v.isValid('http://127.0.0.1/')).resolves.toBe(true), expect(v.isValid('https://www.github.com/')).resolves.toBe(true), + expect(v.isValid('http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]')).resolves.toBe(true), + expect(v.isValid('ftp://localhost')).resolves.toBe(true), + expect(v.isValid('http://[::255.255.255.255]')).resolves.toBe(true), + expect(v.isValid('http://localhost/')).resolves.toBe(true), + expect(v.isValid('http://localhost:8000/')).resolves.toBe(true), + expect(v.isValid('//T.' + '0.'.repeat(3000) + '\x00')).resolves.toBe(false), expect(v.isValid('this is not a url')).resolves.toBe(false), ]); });