Skip to content

Commit

Permalink
Merge pull request #266 from Shelf-nu/custom-email-regex-validation
Browse files Browse the repository at this point in the history
replacing zod's default email validation with a custom refine function
  • Loading branch information
DonKoko authored Aug 1, 2023
2 parents ac6022a + 665b495 commit 9591220
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
8 changes: 5 additions & 3 deletions app/routes/_auth+/forgot-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Button } from "~/components/shared/button";
import { db } from "~/database";

import { getAuthSession, sendResetPasswordLink } from "~/modules/auth";
import { assertIsPost, isFormProcessing, tw } from "~/utils";
import { assertIsPost, isFormProcessing, tw, validEmail } from "~/utils";
import { appendToMetaTitle } from "~/utils/append-to-meta-title";

export async function loader({ request }: LoaderArgs) {
Expand All @@ -24,8 +24,10 @@ export async function loader({ request }: LoaderArgs) {
const ForgotPasswordSchema = z.object({
email: z
.string()
.email("Please enter a valid Email address")
.transform((email) => email.toLowerCase()),
.transform((email) => email.toLowerCase())
.refine(validEmail, () => ({
message: "Please enter a valid email",
})),
});

export async function action({ request }: ActionArgs) {
Expand Down
8 changes: 5 additions & 3 deletions app/routes/_auth+/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
signInWithEmail,
ContinueWithEmailForm,
} from "~/modules/auth";
import { assertIsPost, isFormProcessing } from "~/utils";
import { assertIsPost, isFormProcessing, validEmail } from "~/utils";
import { appendToMetaTitle } from "~/utils/append-to-meta-title";

export async function loader({ request }: LoaderArgs) {
Expand All @@ -34,8 +34,10 @@ export async function loader({ request }: LoaderArgs) {
const LoginFormSchema = z.object({
email: z
.string()
.email("Please enter a valid email.")
.transform((email) => email.toLowerCase()),
.transform((email) => email.toLowerCase())
.refine(validEmail, () => ({
message: "Please enter a valid email",
})),
password: z.string().min(8, "Password is too short. Minimum 8 characters."),
redirectTo: z.string().optional(),
});
Expand Down
13 changes: 11 additions & 2 deletions app/routes/_auth+/send-magic-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { parseFormAny } from "react-zorm";
import { z } from "zod";

import { sendMagicLink } from "~/modules/auth";
import { validEmail } from "~/utils";
import { assertIsPost } from "~/utils/http.server";

export async function action({ request }: ActionArgs) {
Expand All @@ -12,10 +13,18 @@ export async function action({ request }: ActionArgs) {
const formData = await request.formData();
const result = await z
.object({
/**
* .email() has an issue with validating email
* addresses where the there is a subdomain and a dash included:
* https://github.com/colinhacks/zod/pull/2157
* So we use the custom validation
* */
email: z
.string()
.email("Please enter a valid email.")
.transform((email) => email.toLowerCase()),
.transform((email) => email.toLowerCase())
.refine(validEmail, () => ({
message: "Please enter a valid email",
})),
})
.safeParseAsync(parseFormAny(formData));

Expand Down
1 change: 1 addition & 0 deletions app/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from "./gif-to-png";
export * from "./slugify";
export * from "./geolocate.server";
export * from "./user-friendly-asset-status";
export * from "./misc";
8 changes: 8 additions & 0 deletions app/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* .email() has an issue with validating email
* addresses where the there is a subdomain and a dash included:
* https://github.com/colinhacks/zod/pull/2157
* So we use the custom validation
* */
export const validEmail = (val: string) =>
/^([A-Z0-9_+-]+\.?)*[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i.test(val);

0 comments on commit 9591220

Please sign in to comment.