You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
15 lines
403 B
TypeScript
15 lines
403 B
TypeScript
import type { ZodType } from "zod";
|
|
|
|
export function safeParseWithSchema<T>(schema: ZodType<T>, value: unknown): T | null {
|
|
const parsed = schema.safeParse(value);
|
|
return parsed.success ? parsed.data : null;
|
|
}
|
|
|
|
export function safeParseJsonWithSchema<T>(schema: ZodType<T>, raw: string): T | null {
|
|
try {
|
|
return safeParseWithSchema(schema, JSON.parse(raw));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|