mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 22:53:25 +00:00
feat: add support for domain without extension (ie: localhost)
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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:";
|
||||
}
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user