mirror of
https://github.com/linsa-io/linsa.git
synced 2026-04-24 01:08:36 +02:00
Move to TanStack Start from Next.js (#184)
This commit is contained in:
43
web/app/lib/utils/schema.ts
Normal file
43
web/app/lib/utils/schema.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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",
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user