Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use regex for .url() #3497

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 96 additions & 5 deletions deno/lib/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,102 @@ test("base64 validations", () => {

test("url validations", () => {
const url = z.string().url();
url.parse("https://google.com");
url.parse("https://google.com/asdf?asdf=ljk3lk4&asdf=234#asdf");
expect(() => url.parse("asdf")).toThrow();
expect(() => url.parse("https:/")).toThrow();
expect(() => url.parse("[email protected]")).toThrow();
for (const el of [
"https://foo.com/blah_blah",
"https://foo.com/blah_blah/",
"https://foo.com/blah_blah_(wikipedia)",
"https://foo.com/blah_blah_(wikipedia)_(again)",
"https://www.example.com/wpstyle/?p=364",
"https://www.example.com/foo/?bar=baz&inga=42&quux",
"https://✪df.ws/123",
"https://userid:[email protected]:8080",
"https://userid:[email protected]:8080/",
"https://[email protected]",
"https://[email protected]/",
"https://[email protected]:8080",
"https://[email protected]:8080/",
"https://userid:[email protected]",
"https://userid:[email protected]/",
"https://➡.ws/䨹",
"https://⌘.ws",
"https://⌘.ws/",
"https://foo.com/blah_(wikipedia)#cite-1",
"https://foo.com/blah_(wikipedia)_blah#cite-1",
"https://foo.com/unicode_(✪)_in_parens",
"https://foo.com/(something)?after=parens",
"https://☺.damowmow.com/",
"https://code.google.com/events/#&product=browser",
"https://j.mp",
"ftp:https://foo.bar/baz",
"https://foo.bar/?q=Test%20URL-encoded%20stuff",
"https://مثال.إختبار",
"https://例子.测试",
"https://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
"https://1337.net",
"https://a.b-c.de",
"https://a.b--c.de",
"https://me.com//123", // Double slashes are valid according to RFC 2396
"https://223.255.255.254",
"https://www.google.be",
"https://www.google.be/?q=test",
"https://www.google.be?q=test",
"https://www.google.be?q=test&hi=true",
"https://www.google.com/?a=1&b=2",
"https://www.google.com/#4802394",
"https://www.google.com/#inbox?a=2",
"https://google.com/#floo_bar?a=2&c=foo",
"https://www.google.co.uk:3000/eweqweeqwe/ewqeeqweqe",
"https://www.google.co.uk/eweqweeqwe/ewqeeqweqe",
"https://google.com/?a=1&b=2#eweqweqwewqeqweweqweqweqweqwewopqieqwoeqweipqwoeiqweiqwpoeiqwieqwopeiqwpooeqwieiqwoeiqwoeiqweiqwopeiqwpeiqweiqweoiwqopepwoqiepqwoieoiwqeipqwoeiowqieoipoeiwqoiepowiqoieqw&ewquie", // eslint-disable-line max-len
"https://google.com:3000/?a=1&b=2#eweqweqwewqeqweweqweqweqweqwewopqieqwoeqweipqwoeiqweiqwpoeiqwieqwopeiqwpooeqwieiqwoeiqwoeiqweiqwopeiqwpeiqweiqweoiwqopepwoqiepqwoieoiwqeipqwoeiowqieoipoeiwqoiepowiqoieqw&ewquie", // eslint-disable-line max-len
"https://x.comddfsdfsdf.", // Trailing dots in tlds are valid
]) {
expect(() => url.parse(el)).not.toThrow();
}

for (const el of [
"asdf",
"[email protected]",
"https://",
"https://.",
"https://..",
"https://../",
"https://?",
"https://??",
"https://??/",
"https://#",
"https://##",
"https://##/",
"https://foo.bar?q=Spaces should be encoded",
"//",
"//a",
"///a",
"///",
"https:///a",
"foo.com",
"rdar:https://1234",
"h:https://test",
"https:// shouldfail.com",
":https:// should fail",
"https://foo.bar/foo(bar)baz quux",
"ftps:https://foo.bar/",
"https://-error-.invalid/",
"https://-a.b.co",
"https://a.b-.co",
"https://0.0.0.0",
"https://10.1.1.0",
"https://10.1.1.255",
"https://224.1.1.1",
"https://1.1.1.1.1",
"https://123.123.123",
"https://3628126748",
"https://.www.foo.bar/",
"https://.www.foo.bar./",
"https://10.1.1.1",
"https://10.1.1.254",
]) {
expect(() => url.parse(el)).toThrow();
}
});

test("url error overrides", () => {
Expand Down
5 changes: 2 additions & 3 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ const uuidRegex =
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
const durationRegex =
/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
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-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i;

// from https://stackoverflow.com/a/46181/1550155
// old version: too slow, didn't support unicode
Expand Down Expand Up @@ -816,9 +817,7 @@ export class ZodString extends ZodType<string, ZodStringDef, string> {
status.dirty();
}
} else if (check.kind === "url") {
try {
new URL(input.data);
} catch {
if (!urlRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "url",
Expand Down
101 changes: 96 additions & 5 deletions src/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,102 @@ test("base64 validations", () => {

test("url validations", () => {
const url = z.string().url();
url.parse("https://google.com");
url.parse("https://google.com/asdf?asdf=ljk3lk4&asdf=234#asdf");
expect(() => url.parse("asdf")).toThrow();
expect(() => url.parse("https:/")).toThrow();
expect(() => url.parse("[email protected]")).toThrow();
for (const el of [
"https://foo.com/blah_blah",
"https://foo.com/blah_blah/",
"https://foo.com/blah_blah_(wikipedia)",
"https://foo.com/blah_blah_(wikipedia)_(again)",
"https://www.example.com/wpstyle/?p=364",
"https://www.example.com/foo/?bar=baz&inga=42&quux",
"https://✪df.ws/123",
"https://userid:[email protected]:8080",
"https://userid:[email protected]:8080/",
"https://[email protected]",
"https://[email protected]/",
"https://[email protected]:8080",
"https://[email protected]:8080/",
"https://userid:[email protected]",
"https://userid:[email protected]/",
"https://➡.ws/䨹",
"https://⌘.ws",
"https://⌘.ws/",
"https://foo.com/blah_(wikipedia)#cite-1",
"https://foo.com/blah_(wikipedia)_blah#cite-1",
"https://foo.com/unicode_(✪)_in_parens",
"https://foo.com/(something)?after=parens",
"https://☺.damowmow.com/",
"https://code.google.com/events/#&product=browser",
"https://j.mp",
"ftp:https://foo.bar/baz",
"https://foo.bar/?q=Test%20URL-encoded%20stuff",
"https://مثال.إختبار",
"https://例子.测试",
"https://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
"https://1337.net",
"https://a.b-c.de",
"https://a.b--c.de",
"https://me.com//123", // Double slashes are valid according to RFC 2396
"https://223.255.255.254",
"https://www.google.be",
"https://www.google.be/?q=test",
"https://www.google.be?q=test",
"https://www.google.be?q=test&hi=true",
"https://www.google.com/?a=1&b=2",
"https://www.google.com/#4802394",
"https://www.google.com/#inbox?a=2",
"https://google.com/#floo_bar?a=2&c=foo",
"https://www.google.co.uk:3000/eweqweeqwe/ewqeeqweqe",
"https://www.google.co.uk/eweqweeqwe/ewqeeqweqe",
"https://google.com/?a=1&b=2#eweqweqwewqeqweweqweqweqweqwewopqieqwoeqweipqwoeiqweiqwpoeiqwieqwopeiqwpooeqwieiqwoeiqwoeiqweiqwopeiqwpeiqweiqweoiwqopepwoqiepqwoieoiwqeipqwoeiowqieoipoeiwqoiepowiqoieqw&ewquie", // eslint-disable-line max-len
"https://google.com:3000/?a=1&b=2#eweqweqwewqeqweweqweqweqweqwewopqieqwoeqweipqwoeiqweiqwpoeiqwieqwopeiqwpooeqwieiqwoeiqwoeiqweiqwopeiqwpeiqweiqweoiwqopepwoqiepqwoieoiwqeipqwoeiowqieoipoeiwqoiepowiqoieqw&ewquie", // eslint-disable-line max-len
"https://x.comddfsdfsdf.", // Trailing dots in tlds are valid
]) {
expect(() => url.parse(el)).not.toThrow();
}

for (const el of [
"asdf",
"[email protected]",
"https://",
"https://.",
"https://..",
"https://../",
"https://?",
"https://??",
"https://??/",
"https://#",
"https://##",
"https://##/",
"https://foo.bar?q=Spaces should be encoded",
"//",
"//a",
"///a",
"///",
"https:///a",
"foo.com",
"rdar:https://1234",
"h:https://test",
"https:// shouldfail.com",
":https:// should fail",
"https://foo.bar/foo(bar)baz quux",
"ftps:https://foo.bar/",
"https://-error-.invalid/",
"https://-a.b.co",
"https://a.b-.co",
"https://0.0.0.0",
"https://10.1.1.0",
"https://10.1.1.255",
"https://224.1.1.1",
"https://1.1.1.1.1",
"https://123.123.123",
"https://3628126748",
"https://.www.foo.bar/",
"https://.www.foo.bar./",
"https://10.1.1.1",
"https://10.1.1.254",
]) {
expect(() => url.parse(el)).toThrow();
}
});

test("url error overrides", () => {
Expand Down
5 changes: 2 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ const uuidRegex =
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
const durationRegex =
/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
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-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i;

// from https://stackoverflow.com/a/46181/1550155
// old version: too slow, didn't support unicode
Expand Down Expand Up @@ -816,9 +817,7 @@ export class ZodString extends ZodType<string, ZodStringDef, string> {
status.dirty();
}
} else if (check.kind === "url") {
try {
new URL(input.data);
} catch {
if (!urlRegex.test(input.data)) {
ctx = this._getOrReturnCtx(input, ctx);
addIssueToContext(ctx, {
validation: "url",
Expand Down