fix: Relax URL validation (#7336)

This commit is contained in:
Michael Genson
2026-03-27 10:33:33 -05:00
committed by GitHub
parent c029a639fb
commit fc8b1f3719

View File

@@ -3,8 +3,6 @@ import { useGlobalI18n } from "~/composables/use-global-i18n";
const EMAIL_REGEX
= /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const URL_REGEX = /[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/;
export function required(v: string | undefined | null) {
const i18n = useGlobalI18n();
return !!v || i18n.t("validators.required");
@@ -22,7 +20,17 @@ export function whitespace(v: string | null | undefined) {
export function url(v: string | undefined | null) {
const i18n = useGlobalI18n();
return (!!v && URL_REGEX.test(v) && (v.startsWith("http://") || v.startsWith("https://"))) || i18n.t("validators.invalid-url");
if (!v) {
return i18n.t("validators.invalid-url");
}
try {
const parsed = new URL(v);
return (parsed.protocol === "http:" || parsed.protocol === "https:") || i18n.t("validators.invalid-url");
}
catch {
return i18n.t("validators.invalid-url");
}
}
export function urlOptional(v: string | undefined | null) {