mirror of
https://github.com/linsa-io/linsa.git
synced 2026-04-19 23:11:30 +02:00
fix: link (#115)
* start * . * seeding connections * . * wip * wip: learning state * wip: notes section * wip: many * topics * chore: update schema * update package * update sidebar * update page section * feat: profile * fix: remove z index * fix: wrong type * add avatar * add avatar * wip * . * store page section key * remove atom page section * fix rerender * fix rerender * fix rerender * fix rerender * fix link * search light/dark mode * bubble menu ui * . * fix: remove unecessary code * chore: mark as old for old schema * chore: adapt new schema * fix: add topic schema but null for now * fix: add icon on personal link * fix: list item * fix: set url fetched when editing * fix: remove image * feat: add icon to link * feat: custom url zod validation * fix: metadata test * chore: update utils * fix: link * fix: url fetcher * . * . * fix: add link, section * chore: seeder * . * . * . * . * fix: change checkbox to learning state * fix: click outside editing form * feat: constant * chore: move to master folder * chore: adapt new schema * chore: cli for new schema * fix: new schema for dev seed * fix: seeding * update package * chore: forcegraph seed * bottombar * if isEdit delete icon * showCreate X button * . * options * chore: implement topic from public global group * chore: update learning state * fix: change implementation for outside click * chore: implement new form param * chore: update env example * feat: link form refs exception * new page button layout, link topic search fixed * chore: enable topic * chore: update seed * profile * chore: move framer motion package from root to web and add nuqs * chore: add LearningStateValue * chore: implement active state * profile * chore: use fancy switch and update const * feat: filter implementation * dropdown menu * . * sidebar topics * topic selected color * feat: topic detail * fix: collapsible page * pages - sorted by, layout, visible mode * . * . * . * topic status sidebar * topic button and count * fix: topic * page delete/topic buttons * search ui * selected topic for page * selected topic status sidebar * removed footer * update package * . --------- Co-authored-by: Nikita <github@nikiv.dev> Co-authored-by: marshennikovaolga <marshennikova@gmail.com> Co-authored-by: Kisuyo <ig.intr3st@gmail.com>
This commit is contained in:
39
web/lib/utils/schema.test.ts
Normal file
39
web/lib/utils/schema.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { urlSchema } from "./schema"
|
||||
|
||||
function validateUrl(url: string): boolean {
|
||||
const result = urlSchema.safeParse(url)
|
||||
return result.success
|
||||
}
|
||||
|
||||
describe("URL Validation", () => {
|
||||
test("valid full URLs", () => {
|
||||
expect(validateUrl("https://www.example.com")).toBe(true)
|
||||
expect(validateUrl("http://example.com")).toBe(true)
|
||||
expect(validateUrl("https://subdomain.example.co.uk")).toBe(true)
|
||||
})
|
||||
|
||||
test("valid domain names without protocol", () => {
|
||||
expect(validateUrl("aslamh.com")).toBe(true)
|
||||
expect(validateUrl("www.example.com")).toBe(true)
|
||||
expect(validateUrl("subdomain.example.co.uk")).toBe(true)
|
||||
})
|
||||
|
||||
test("invalid URLs", () => {
|
||||
expect(validateUrl("https://aslamh")).toBe(false)
|
||||
expect(validateUrl("ftp://example.com")).toBe(false)
|
||||
expect(validateUrl("http://.com")).toBe(false)
|
||||
expect(validateUrl("just-a-string")).toBe(false)
|
||||
expect(validateUrl("aslamh")).toBe(false)
|
||||
})
|
||||
|
||||
test("edge cases", () => {
|
||||
expect(validateUrl("https://localhost")).toBe(false) // No TLD
|
||||
expect(validateUrl("https://123.45.67.89")).toBe(false) // IP address
|
||||
expect(validateUrl("https://example.com/path?query=value")).toBe(true) // With path and query
|
||||
})
|
||||
|
||||
test("empty and whitespace inputs", () => {
|
||||
expect(validateUrl("")).toBe(false)
|
||||
expect(validateUrl(" ")).toBe(false)
|
||||
})
|
||||
})
|
||||
42
web/lib/utils/schema.ts
Normal file
42
web/lib/utils/schema.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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"
|
||||
}
|
||||
)
|
||||
@@ -12,10 +12,10 @@ export function isUrl(text: string): boolean {
|
||||
return pattern.test(text)
|
||||
}
|
||||
|
||||
export function ensureUrlProtocol(url: string): string {
|
||||
if (url.startsWith("http://") || url.startsWith("https://")) {
|
||||
export function ensureUrlProtocol(url: string, defaultProtocol: string = "https://"): string {
|
||||
if (url.match(/^[a-zA-Z]+:\/\//)) {
|
||||
return url
|
||||
}
|
||||
|
||||
return `https://${url}`
|
||||
return `${defaultProtocol}${url.startsWith("//") ? url.slice(2) : url}`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user