feat: add support for domain without extension (ie: localhost)

This commit is contained in:
Sonny
2023-11-23 14:02:32 +01:00
parent 5fe58c44d5
commit d4af0b4a4d
6 changed files with 18 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
import { VALID_URL_REGEX } from "constants/url";
import { boolean, number, object, string } from "yup";
import { isValidHttpUrl } from "../url";
const LinkBodySchema = object({
name: string()
@@ -9,7 +9,7 @@ const LinkBodySchema = object({
url: string()
.trim()
.required("URl is required")
.matches(VALID_URL_REGEX, "Invalid URL format"),
.test("test_url", "Invalid URL format", (value) => isValidHttpUrl(value)),
categoryId: number().required("CategoryId must be a number"),
favorite: boolean().default(() => false),
}).typeError("Missing request Body");

View File

@@ -47,3 +47,15 @@ export function findFaviconPath(text: string) {
return favicon?.getAttribute("href") || undefined;
}
export function isValidHttpUrl(urlParam: string) {
let url;
try {
url = new URL(urlParam);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}

View File

@@ -1,7 +1,6 @@
import { apiHandler } from "lib/api/handler";
import getUserLink from "lib/link/getUserLink";
import { LinkBodySchema, LinkQuerySchema } from "lib/link/linkValidationSchema";
import prisma from "utils/prisma";
export default apiHandler({

View File

@@ -12,9 +12,9 @@ import { useRouter } from "next/router";
import { FormEvent, useMemo, useState } from "react";
import styles from "styles/form.module.scss";
import { Category, Link } from "types";
import { IsValidURL } from "utils/front";
import { withAuthentication } from "utils/session";
import { makeRequest } from "lib/request";
import { isValidHttpUrl } from "lib/url";
export default function PageCreateLink({
categories,
@@ -39,7 +39,7 @@ export default function PageCreateLink({
const canSubmit = useMemo<boolean>(
() =>
name !== "" &&
IsValidURL(url) &&
isValidHttpUrl(url) &&
favorite !== null &&
categoryId !== null &&
!submitted,

View File

@@ -13,7 +13,7 @@ import { useRouter } from "next/router";
import { FormEvent, useMemo, useState } from "react";
import styles from "styles/form.module.scss";
import { Category, Link } from "types";
import { IsValidURL } from "utils/front";
import { isValidHttpUrl } from "lib/url";
import { withAuthentication } from "utils/session";
import { makeRequest } from "lib/request";
@@ -46,7 +46,7 @@ export default function PageEditLink({
categoryId !== link.category.id;
const isFormValid =
name !== "" &&
IsValidURL(url) &&
isValidHttpUrl(url) &&
favorite !== null &&
categoryId !== null;
return isFormEdited && isFormValid && !submitted;

View File

@@ -1,6 +0,0 @@
import { VALID_URL_REGEX } from "constants/url";
export function IsValidURL(url: string): boolean {
const regex = new RegExp(VALID_URL_REGEX);
return !!url.match(regex);
}