Files
archived-linsa/web/app/lib/utils/schema.ts
2024-10-07 12:44:17 +03:00

44 lines
872 B
TypeScript

/*
* This file contains custom schema definitions for Zod.
*/
import { z } from "zod"
export const urlSchema = z
.string()
.min(1, { message: "URL can't be empty" })
.refine(
(value) => {
const domainRegex =
/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/
const isValidDomain = (domain: string) => {
try {
const url = new URL(`http://${domain}`)
return domainRegex.test(url.hostname)
} catch {
return false
}
}
if (isValidDomain(value)) {
return true
}
try {
const url = new URL(value)
if (!url.protocol.match(/^https?:$/)) {
return false
}
return isValidDomain(url.hostname)
} catch {
return false
}
},
{
message: "Please enter a valid URL",
},
)