zod-to-x
Version:
Multi language types generation from Zod schemas.
21 lines (20 loc) • 1.27 kB
TypeScript
import { ZodTypeAny } from "zod";
/**
* Recursively extracts custom type definitions from a Zod schema and collects them into a
* definitions object.
*
* This function traverses the provided Zod schema and its nested schemas to collect all schemas
* that have a custom `typeName` defined in `schema._zod2x.typeName`. It accumulates these
* schemas into a `definitions` object, which maps each `typeName` to its corresponding schema.
* This is useful for generating JSON Schema definitions or for any scenario where you need to
* collect and reference custom types.
*
* @param schema - The root Zod schema from which to extract definitions.
* @param definitions - (Optional) An object to accumulate the definitions.
* Used across recursive calls.
* @param visited - (Optional) A Set to keep track of visited schemas and prevent infinite recursion
* in cases of circular references. Used across recursive calls.
* @returns An object containing all extracted definitions, where each key is a `typeName` and the
* value is the corresponding Zod schema.
*/
export declare function zod2JsonSchemaDefinitions(schema: ZodTypeAny, definitions?: Record<string, any>, visited?: Set<ZodTypeAny>): Record<string, any>;