Skip to content

Instantly share code, notes, and snippets.

View zAlweNy26's full-sized avatar
🏠
Working from home

Daniele Nicosia zAlweNy26

🏠
Working from home
View GitHub Profile
@zAlweNy26
zAlweNy26 / getZodDefaults.ts
Last active October 7, 2025 19:14
Initialize Zod v4 schema with defaults
/**
* Retrieves the default values for a given Zod schema.
* @param schema The Zod schema for which to retrieve the default values.
* @param defaults A partial object containing default values for the schema.
* @param discriminant The discriminant value for discriminated unions.
* @returns An object containing the default values for the schema, or `undefined` if no defaults are found.
*/
export function getZodDefaults<T extends z.ZodType>(schema: T, defaults?: Partial<T['_output']>, discriminant?: string): T['_output'] | undefined {
if (schema instanceof z.ZodDefault) {
const def = schema.def.defaultValue
@zAlweNy26
zAlweNy26 / tryCatch.ts
Created September 12, 2025 08:48
My custom tryCatch implementation
/**
* Catches errors from a promise.
* @param promise The promise to handle.
* @param options Additional options for handling the promise.
* @returns A tuple with either the error or the result of the promise.
* @throws Will rethrow the error if it is not in the `errorsToCatch` array.
*/
export async function tryCatch<T = any, E extends new (...args: any[]) => Error = ErrorConstructor>(
promise: Promise<T>,
options?: {