Files
openclaw/src/utils/zod-parse.ts
2026-03-27 03:41:40 +00:00

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;
}
}