/** * Find ZodDefault in the wrapper chain and return its default value. */ export const getZodDefaultValue = (field: unknown): unknown | undefined => { let current = field; const visited = new Set(); while (current && !visited.has(current)) { visited.add(current); const currentWithDef = current as { _def?: { typeName?: string; defaultValue?: () => unknown; innerType?: unknown; }; }; if (!currentWithDef._def?.typeName) break; if (currentWithDef._def.typeName === 'ZodDefault') { return currentWithDef._def.defaultValue?.(); } if ( currentWithDef._def.typeName === 'ZodOptional' || currentWithDef._def.typeName === 'ZodNullable' ) { current = currentWithDef._def.innerType; } else { break; } } return undefined; };