Files
pocket-id/frontend/src/lib/utils/zod-util.ts
2025-09-17 14:43:12 -05:00

37 lines
812 B
TypeScript

import { m } from '$lib/paraglide/messages';
import { z } from 'zod/v4';
export const emptyToUndefined = <T>(validation: z.ZodType<T>) =>
z.preprocess((v) => (v === '' ? undefined : v), validation.optional());
export const optionalUrl = z
.url()
.optional()
.or(z.literal('').transform(() => undefined));
export const callbackUrlSchema = z
.string()
.nonempty()
.refine(
(val) => {
if (val === '*') return true;
try {
new URL(val.replace(/\*/g, 'x'));
return true;
} catch {
return false;
}
},
{
message: m.invalid_redirect_url()
}
);
export const usernameSchema = z
.string()
.min(2)
.max(30)
.regex(/^[a-zA-Z0-9]/, m.username_must_start_with())
.regex(/[a-zA-Z0-9]$/, m.username_must_end_with())
.regex(/^[a-zA-Z0-9_.@-]+$/, m.username_can_only_contain());