Adjusted the character set used by nanoid to exclude ambiguous characters, improving readability and reducing potential user confusion.
25 lines
1.1 KiB
TypeScript
25 lines
1.1 KiB
TypeScript
import { z } from 'zod'
|
|
import { customAlphabet } from 'nanoid'
|
|
|
|
const { slugRegex } = useAppConfig()
|
|
|
|
const slugDefaultLength = +useRuntimeConfig().public.slugDefaultLength
|
|
|
|
export const nanoid = (length: number = slugDefaultLength) => customAlphabet('23456789abcdefghjklmnpqrstuvwxyz', length)
|
|
|
|
export const LinkSchema = z.object({
|
|
id: z.string().trim().max(26).default(nanoid(10)),
|
|
url: z.string().trim().url().max(2048),
|
|
slug: z.string().trim().max(2048).regex(new RegExp(slugRegex)).default(nanoid()),
|
|
comment: z.string().trim().max(2048).optional(),
|
|
createdAt: z.number().int().safe().default(() => Math.floor(Date.now() / 1000)),
|
|
updatedAt: z.number().int().safe().default(() => Math.floor(Date.now() / 1000)),
|
|
expiration: z.number().int().safe().refine(expiration => expiration > Math.floor(Date.now() / 1000), {
|
|
message: 'expiration must be greater than current time',
|
|
path: ['expiration'], // 这里指定错误消息关联到哪个字段
|
|
}).optional(),
|
|
title: z.string().trim().max(2048).optional(),
|
|
description: z.string().trim().max(2048).optional(),
|
|
image: z.string().trim().url().max(2048).optional(),
|
|
})
|