From 79c698e72b1a1e648bcc074c511cb12dd045a899 Mon Sep 17 00:00:00 2001 From: AmirAgassi <33383085+AmirAgassi@users.noreply.github.com> Date: Fri, 26 Apr 2024 20:35:34 -0400 Subject: [PATCH] Replace custom URL validation with zod --- src/components/forms/hackerApplication.ts | 9 ++------- src/pages/Application/Application.page.tsx | 8 +------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/components/forms/hackerApplication.ts b/src/components/forms/hackerApplication.ts index a868fa84..f20a742d 100644 --- a/src/components/forms/hackerApplication.ts +++ b/src/components/forms/hackerApplication.ts @@ -12,15 +12,10 @@ import { volunteerSpecificOptions, } from "@data"; import type { FormInput } from "./types"; +import * as z from 'zod'; function isValidUrl(url: string) { - const pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol - '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name - '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address (you never know!!) - '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path - '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string - '(\\#[-a-z\\d_]*)?$','i'); // fragment locator - return !!pattern.test(url); + return z.string().url().safeParse(url).success; } export const hackerAppFormInputs: FormInput[] = [ diff --git a/src/pages/Application/Application.page.tsx b/src/pages/Application/Application.page.tsx index 10443568..93b73ba3 100644 --- a/src/pages/Application/Application.page.tsx +++ b/src/pages/Application/Application.page.tsx @@ -63,13 +63,7 @@ function getLogEventName(component: string) { } function isValidUrl(url: string) { - const pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol - '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name - '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address (you never know!!) - '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path - '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string - '(\\#[-a-z\\d_]*)?$','i'); // fragment locator - return !!pattern.test(url); + return z.string().url().safeParse(url).success; } export const ApplicationPage = () => {