Last active
October 7, 2025 19:14
-
-
Save zAlweNy26/a18db347455614c39fc1a010ad3cadf2 to your computer and use it in GitHub Desktop.
Initialize Zod v4 schema with defaults
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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 | |
| return typeof def === 'function' ? def() : def | |
| } | |
| else if (schema instanceof z.ZodLiteral) return [...schema.values][0] | |
| else if (schema instanceof z.ZodDiscriminatedUnion) { | |
| if (!discriminant) throw new Error('Discriminant value is required for discriminated unions') | |
| const discriminator = schema._zod.def.discriminator | |
| for (const opt of schema.options) { | |
| const shape = (opt as z.ZodObject).def.shape | |
| const literals = (shape[discriminator] as z.ZodLiteral).values | |
| if (literals.has(discriminant)) return getZodDefaults(opt as any, defaults, discriminant) | |
| } | |
| } | |
| else if (schema instanceof z.ZodObject) { | |
| const shape = schema.def.shape | |
| const result: any = {} | |
| for (const key in shape) { | |
| const value = getZodDefaults(shape[key], typeof defaults === 'object' ? (defaults as any)[key] : defaults, discriminant) | |
| result[key] = defaults?.[key as keyof typeof defaults] ?? value | |
| } | |
| return result | |
| } | |
| else if (schema instanceof z.ZodIntersection) { | |
| const left = getZodDefaults(schema.def.left as any, defaults, discriminant) | |
| const right = getZodDefaults(schema.def.right as any, defaults, discriminant) | |
| return { ...left, ...right } | |
| } | |
| else if (schema instanceof z.ZodArray || schema instanceof z.ZodTuple) return [] | |
| else if (schema instanceof z.ZodUnion) { | |
| for (const val of schema.options) { | |
| const value = getZodDefaults(val as any, defaults, discriminant) | |
| if (value !== undefined) return value | |
| } | |
| return undefined | |
| } | |
| else if (schema instanceof z.ZodRecord) return {} | |
| else if (schema instanceof z.ZodMap) return new Map() | |
| else if (schema instanceof z.ZodSet) return new Set() | |
| else return undefined | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment