@mastra/core
Version:
77 lines • 3.31 kB
TypeScript
import type { z } from 'zod/v4';
type ZodTypeAny = z.ZodType<any, any>;
type ZodObjectAny = z.ZodObject<any>;
type ZodArrayAny = z.ZodArray<any>;
/**
* Checks if a value is a Zod type
* @param value - The value to check
* @returns True if the value is a Zod type, false otherwise
*/
export declare function isZodType(value: unknown): value is ZodTypeAny;
/**
* Get the Zod typeName from a schema, compatible with both Zod 3 and Zod 4.
* Uses string-based typeName instead of instanceof to avoid dual-package hazard
* where multiple Zod instances can cause instanceof checks to fail.
*
* Zod 3 uses `_def.typeName` with values like "ZodString", "ZodOptional", etc.
* Zod 4 uses `_def.type` with lowercase values like "string", "optional", etc.
*
* This function normalizes to Zod 3 format (e.g., "ZodString") for compatibility.
*
* @param schema - The Zod schema to get the type name from
* @returns The Zod type name string (e.g., "ZodString", "ZodOptional") or undefined
*/
export declare function getZodTypeName(schema: ZodTypeAny): string | undefined;
/**
* Check if a value is a ZodArray type
* @param value - The value to check (can be any type)
* @returns True if the value is a ZodArray
*/
export declare function isZodArray(value: unknown): value is ZodArrayAny;
/**
* Check if a value is a ZodObject type
* @param value - The value to check (can be any type)
* @returns True if the value is a ZodObject
*/
export declare function isZodObject(value: unknown): value is ZodObjectAny;
/**
* Add fields to a ZodObject, compatible with both Zod 3 and Zod 4.
*
* Zod 4's `.extend()` throws ("Cannot overwrite keys on object schemas containing
* refinements. Use `.safeExtend()` instead.") when overwriting a key on a schema that
* carries a `.refine()`/`.superRefine()` check. `.safeExtend()` is the v4
* escape hatch and keeps the refinement; Zod 3 has neither the restriction nor
* `.safeExtend()`, so we fall back to `.extend()` there.
*
* @param schema - The ZodObject to extend
* @param shape - The fields to add
* @returns The extended ZodObject
*/
export declare function safeExtendZodObject(schema: ZodObjectAny, shape: Record<string, ZodTypeAny>): ZodObjectAny;
/**
* Get the def object from a Zod schema, compatible with both Zod 3 and Zod 4.
* @param schema - The Zod schema
* @returns The def object
*/
export declare function getZodDef(schema: ZodTypeAny): any;
/**
* Get the inner type from a wrapper schema (nullable, optional, default, effects, branded).
* Compatible with both Zod 3 and Zod 4.
*
* @param schema - The wrapper Zod schema
* @param typeName - The Zod type name of the wrapper (e.g., "ZodOptional")
* @returns The inner schema, or undefined if not found
*/
export declare function getZodInnerType(schema: z.ZodTypeAny, typeName: string): z.ZodTypeAny | undefined;
/**
* Unwraps Zod wrapper types (optional, nullable, default, effects, branded)
* to find the base schema type. Compatible with both Zod 3 and Zod 4.
*
* For example, `z.array(z.string()).nullish().default([])` unwraps to `z.array(z.string())`.
*
* @param schema - The Zod schema to unwrap
* @returns The innermost base schema
*/
export declare function unwrapZodType(schema: z.ZodTypeAny): z.ZodTypeAny;
export {};
//# sourceMappingURL=zod-utils.d.ts.map