Skip to content

Commit

Permalink
Fix email issues in pull request #1982 (#2058)
Browse files Browse the repository at this point in the history
* Invalidate emails with '.' next to another

* More validation in IPv4 domain

In a valid IPv4 the numbers must go from 0 to 255

* Add IPv6 domain in email validation

* Save selected theme in localStorage
  • Loading branch information
fvckDesa committed Feb 26, 2023
1 parent 8f3d028 commit 5ec98e1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 24 deletions.
19 changes: 19 additions & 0 deletions deno/lib/__tests__/string.test.ts
Expand Up @@ -71,6 +71,14 @@ test("more email validations", () => {
`user@[68.185.127.196]`,
`ipv4@[85.129.96.247]`,
`valid@[79.208.229.53]`,
`valid@[255.255.255.255]`,
`valid@[255.0.55.2]`,
`valid@[255.0.55.2]`,
`hgrebert0@[IPv6:4dc8:ac7:ce79:8878:1290:6098:5c50:1f25]`,
`bshapiro4@[IPv6:3669:c709:e981:4884:59a3:75d1:166b:9ae]`,
`jsmith@[IPv6:2001:db8::1]`,
`postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]`,
`postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:192.168.1.1]`,
];
const invalidEmails = [
`Abc.example.com`,
Expand All @@ -88,6 +96,17 @@ test("more email validations", () => {
`invalid@[68.185.127.196.55]`,
`temp@[192.168.1]`,
`temp@[9.18.122.]`,
`double..point@test.com`,
`asdad@test..com`,
`asdad@hghg...sd...au`,
`asdad@hghg........au`,
`invalid@[256.2.2.48]`,
`invalid@[256.2.2.48]`,
`invalid@[999.465.265.1]`,
`jkibbey4@[IPv6:82c4:19a8::70a9:2aac:557::ea69:d985:28d]`,
`mlivesay3@[9952:143f:b4df:2179:49a1:5e82:b92e:6b6]`,
`gbacher0@[IPv6:bc37:4d3f:5048:2e26:37cc:248e:df8e:2f7f:af]`,
`invalid@[IPv6:5348:4ed3:5d38:67fb:e9b:acd2:c13:192.168.256.1]`,
];
const emailSchema = z.string().email();
expect(
Expand Down
15 changes: 7 additions & 8 deletions deno/lib/types.ts
Expand Up @@ -525,8 +525,7 @@ const uuidRegex =
// eslint-disable-next-line

const emailRegex =
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|([^-]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,}))$/;

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\])|(\[IPv6:(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))\])|([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*(\.[A-Za-z]{2,})+))$/;
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression

const emojiRegex =
Expand Down Expand Up @@ -3502,7 +3501,7 @@ export class ZodFunction<
return this._def.returns;
}

args<Items extends Parameters<typeof ZodTuple["create"]>[0]>(
args<Items extends Parameters<(typeof ZodTuple)["create"]>[0]>(
...items: Items
): ZodFunction<ZodTuple<Items, ZodUnknown>, Returns> {
return new ZodFunction({
Expand Down Expand Up @@ -4620,18 +4619,18 @@ const oboolean = () => booleanType().optional();

export const coerce = {
string: ((arg) =>
ZodString.create({ ...arg, coerce: true })) as typeof ZodString["create"],
ZodString.create({ ...arg, coerce: true })) as (typeof ZodString)["create"],
number: ((arg) =>
ZodNumber.create({ ...arg, coerce: true })) as typeof ZodNumber["create"],
ZodNumber.create({ ...arg, coerce: true })) as (typeof ZodNumber)["create"],
boolean: ((arg) =>
ZodBoolean.create({
...arg,
coerce: true,
})) as typeof ZodBoolean["create"],
})) as (typeof ZodBoolean)["create"],
bigint: ((arg) =>
ZodBigInt.create({ ...arg, coerce: true })) as typeof ZodBigInt["create"],
ZodBigInt.create({ ...arg, coerce: true })) as (typeof ZodBigInt)["create"],
date: ((arg) =>
ZodDate.create({ ...arg, coerce: true })) as typeof ZodDate["create"],
ZodDate.create({ ...arg, coerce: true })) as (typeof ZodDate)["create"],
};

export {
Expand Down
21 changes: 13 additions & 8 deletions index.html
Expand Up @@ -206,22 +206,27 @@

<body>
<div class="theme-btn"></div>

<div id="app"></div>
<script src="//cdnjs.cloudflare.com/ajax/libs/docsify/4.12.2/docsify.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/prism/1.28.0/components/prism-typescript.min.js"></script>
<script>
toggleDarkTheme(localStorage.getItem("dark-theme") === "true" || false);

window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (e) => {
document.body.classList.toggle("dark", e.matches);
});
.addEventListener("change", (e) => toggleDarkTheme(e.matches));

const themeBtn = document.querySelector(".theme-btn");
document
.querySelector(".theme-btn")
.addEventListener("click", () => toggleDarkTheme());

themeBtn.addEventListener("click", () => {
document.body.classList.toggle("dark");
});
function toggleDarkTheme(force) {
document.body.classList.toggle("dark", force);
localStorage.setItem(
"dark-theme",
document.body.classList.contains("dark")
);
}
</script>

<script>
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/string.test.ts
Expand Up @@ -70,6 +70,14 @@ test("more email validations", () => {
`user@[68.185.127.196]`,
`ipv4@[85.129.96.247]`,
`valid@[79.208.229.53]`,
`valid@[255.255.255.255]`,
`valid@[255.0.55.2]`,
`valid@[255.0.55.2]`,
`hgrebert0@[IPv6:4dc8:ac7:ce79:8878:1290:6098:5c50:1f25]`,
`bshapiro4@[IPv6:3669:c709:e981:4884:59a3:75d1:166b:9ae]`,
`jsmith@[IPv6:2001:db8::1]`,
`postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]`,
`postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:192.168.1.1]`,
];
const invalidEmails = [
`Abc.example.com`,
Expand All @@ -87,6 +95,17 @@ test("more email validations", () => {
`invalid@[68.185.127.196.55]`,
`temp@[192.168.1]`,
`temp@[9.18.122.]`,
`double..point@test.com`,
`asdad@test..com`,
`asdad@hghg...sd...au`,
`asdad@hghg........au`,
`invalid@[256.2.2.48]`,
`invalid@[256.2.2.48]`,
`invalid@[999.465.265.1]`,
`jkibbey4@[IPv6:82c4:19a8::70a9:2aac:557::ea69:d985:28d]`,
`mlivesay3@[9952:143f:b4df:2179:49a1:5e82:b92e:6b6]`,
`gbacher0@[IPv6:bc37:4d3f:5048:2e26:37cc:248e:df8e:2f7f:af]`,
`invalid@[IPv6:5348:4ed3:5d38:67fb:e9b:acd2:c13:192.168.256.1]`,
];
const emailSchema = z.string().email();
expect(
Expand Down
15 changes: 7 additions & 8 deletions src/types.ts
Expand Up @@ -525,8 +525,7 @@ const uuidRegex =
// eslint-disable-next-line

const emailRegex =
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|([^-]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,}))$/;

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\])|(\[IPv6:(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))\])|([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*(\.[A-Za-z]{2,})+))$/;
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression

const emojiRegex =
Expand Down Expand Up @@ -3502,7 +3501,7 @@ export class ZodFunction<
return this._def.returns;
}

args<Items extends Parameters<typeof ZodTuple["create"]>[0]>(
args<Items extends Parameters<(typeof ZodTuple)["create"]>[0]>(
...items: Items
): ZodFunction<ZodTuple<Items, ZodUnknown>, Returns> {
return new ZodFunction({
Expand Down Expand Up @@ -4620,18 +4619,18 @@ const oboolean = () => booleanType().optional();

export const coerce = {
string: ((arg) =>
ZodString.create({ ...arg, coerce: true })) as typeof ZodString["create"],
ZodString.create({ ...arg, coerce: true })) as (typeof ZodString)["create"],
number: ((arg) =>
ZodNumber.create({ ...arg, coerce: true })) as typeof ZodNumber["create"],
ZodNumber.create({ ...arg, coerce: true })) as (typeof ZodNumber)["create"],
boolean: ((arg) =>
ZodBoolean.create({
...arg,
coerce: true,
})) as typeof ZodBoolean["create"],
})) as (typeof ZodBoolean)["create"],
bigint: ((arg) =>
ZodBigInt.create({ ...arg, coerce: true })) as typeof ZodBigInt["create"],
ZodBigInt.create({ ...arg, coerce: true })) as (typeof ZodBigInt)["create"],
date: ((arg) =>
ZodDate.create({ ...arg, coerce: true })) as typeof ZodDate["create"],
ZodDate.create({ ...arg, coerce: true })) as (typeof ZodDate)["create"],
};

export {
Expand Down

0 comments on commit 5ec98e1

Please sign in to comment.