UNPKG

@mk-singh/utils

Version:

A collection of TypeScript utilities for Deno including route factory with navigation support

42 lines (41 loc) 1.45 kB
import { z } from "zod/v4"; /** * Extracts default values from a Zod schema with full type safety * @param schema - The Zod schema to extract defaults from * @returns Object containing the default values with proper typing */ export function getZodDefaults(schema) { if (!z) throw new Error("zod is required for getZodDefaults"); const defaults = {}; try { // Try to parse an empty object to get defaults const parsed = schema.parse({}); return parsed; } catch { // If parsing fails, try to extract defaults manually const shape = schema.shape; for (const [key, field] of Object.entries(shape)) { try { const zodField = field; // Try to parse undefined to trigger default const result = zodField.safeParse(undefined); if (result.success) { defaults[key] = result.data; } // Handle nested objects else if (zodField instanceof z.ZodObject) { const nestedDefaults = getZodDefaults(zodField); if (Object.keys(nestedDefaults).length > 0) { defaults[key] = nestedDefaults; } } } catch { // Skip fields that can't be processed continue; } } } return defaults; }