@mastra/core
Version:
1 lines • 6.85 kB
Source Map (JSON)
{"version":3,"file":"zod-utils-BAGXGqPm.cjs","names":[],"sources":["../src/utils/zod-utils.ts"],"sourcesContent":["import type { z } from 'zod/v4';\n\ntype ZodTypeAny = z.ZodType<any, any>;\ntype ZodObjectAny = z.ZodObject<any>;\ntype ZodArrayAny = z.ZodArray<any>;\n\n/**\n * Checks if a value is a Zod type\n * @param value - The value to check\n * @returns True if the value is a Zod type, false otherwise\n */\nexport function isZodType(value: unknown): value is ZodTypeAny {\n // Check if it's a Zod schema by looking for common Zod properties and methods\n return (\n typeof value === 'object' &&\n value !== null &&\n '_def' in value &&\n 'parse' in value &&\n typeof (value as any).parse === 'function' &&\n 'safeParse' in value &&\n typeof (value as any).safeParse === 'function'\n );\n}\n\n/**\n * Get the Zod typeName from a schema, compatible with both Zod 3 and Zod 4.\n * Uses string-based typeName instead of instanceof to avoid dual-package hazard\n * where multiple Zod instances can cause instanceof checks to fail.\n *\n * Zod 3 uses `_def.typeName` with values like \"ZodString\", \"ZodOptional\", etc.\n * Zod 4 uses `_def.type` with lowercase values like \"string\", \"optional\", etc.\n *\n * This function normalizes to Zod 3 format (e.g., \"ZodString\") for compatibility.\n *\n * @param schema - The Zod schema to get the type name from\n * @returns The Zod type name string (e.g., \"ZodString\", \"ZodOptional\") or undefined\n */\nexport function getZodTypeName(schema: ZodTypeAny): string | undefined {\n const schemaAny = schema as any;\n\n // Zod 3 structure: _def.typeName = \"ZodString\", \"ZodOptional\", etc.\n if (schemaAny._def?.typeName) {\n return schemaAny._def.typeName;\n }\n\n // Zod 4 structure: _def.type = \"string\", \"optional\", etc. (lowercase, no prefix)\n const zod4Type = schemaAny._def?.type;\n if (typeof zod4Type === 'string' && zod4Type) {\n // Normalize to Zod 3 format: \"string\" -> \"ZodString\", \"optional\" -> \"ZodOptional\"\n return 'Zod' + zod4Type.charAt(0).toUpperCase() + zod4Type.slice(1);\n }\n\n return undefined;\n}\n\n/**\n * Check if a value is a ZodArray type\n * @param value - The value to check (can be any type)\n * @returns True if the value is a ZodArray\n */\nexport function isZodArray(value: unknown): value is ZodArrayAny {\n if (!isZodType(value)) return false;\n return getZodTypeName(value as ZodTypeAny) === 'ZodArray';\n}\n\n/**\n * Check if a value is a ZodObject type\n * @param value - The value to check (can be any type)\n * @returns True if the value is a ZodObject\n */\nexport function isZodObject(value: unknown): value is ZodObjectAny {\n if (!isZodType(value)) return false;\n return getZodTypeName(value as ZodTypeAny) === 'ZodObject';\n}\n\n/**\n * Add fields to a ZodObject, compatible with both Zod 3 and Zod 4.\n *\n * Zod 4's `.extend()` throws (\"Cannot overwrite keys on object schemas containing\n * refinements. Use `.safeExtend()` instead.\") when overwriting a key on a schema that\n * carries a `.refine()`/`.superRefine()` check. `.safeExtend()` is the v4\n * escape hatch and keeps the refinement; Zod 3 has neither the restriction nor\n * `.safeExtend()`, so we fall back to `.extend()` there.\n *\n * @param schema - The ZodObject to extend\n * @param shape - The fields to add\n * @returns The extended ZodObject\n */\nexport function safeExtendZodObject(schema: ZodObjectAny, shape: Record<string, ZodTypeAny>): ZodObjectAny {\n const extend = (schema as { safeExtend?: ZodObjectAny['extend'] }).safeExtend ?? schema.extend;\n return extend.call(schema, shape);\n}\n\n/**\n * Get the def object from a Zod schema, compatible with both Zod 3 and Zod 4.\n * @param schema - The Zod schema\n * @returns The def object\n */\nexport function getZodDef(schema: ZodTypeAny): any {\n const schemaAny = schema as any;\n return schemaAny._zod?.def ?? schemaAny._def;\n}\n\n/**\n * Get the inner type from a wrapper schema (nullable, optional, default, effects, branded).\n * Compatible with both Zod 3 and Zod 4.\n *\n * @param schema - The wrapper Zod schema\n * @param typeName - The Zod type name of the wrapper (e.g., \"ZodOptional\")\n * @returns The inner schema, or undefined if not found\n */\nexport function getZodInnerType(schema: z.ZodTypeAny, typeName: string): z.ZodTypeAny | undefined {\n const schemaAny = schema as any;\n\n // For nullable, optional, default - the inner type is at _def.innerType\n if (typeName === 'ZodNullable' || typeName === 'ZodOptional' || typeName === 'ZodDefault') {\n return schemaAny._zod?.def?.innerType ?? schemaAny._def?.innerType;\n }\n\n // For effects - the inner type is at _def.schema\n if (typeName === 'ZodEffects') {\n return schemaAny._zod?.def?.schema ?? schemaAny._def?.schema;\n }\n\n // For branded - the inner type is at _def.type\n if (typeName === 'ZodBranded') {\n return schemaAny._zod?.def?.type ?? schemaAny._def?.type;\n }\n\n return undefined;\n}\n\n/**\n * Unwraps Zod wrapper types (optional, nullable, default, effects, branded)\n * to find the base schema type. Compatible with both Zod 3 and Zod 4.\n *\n * For example, `z.array(z.string()).nullish().default([])` unwraps to `z.array(z.string())`.\n *\n * @param schema - The Zod schema to unwrap\n * @returns The innermost base schema\n */\nexport function unwrapZodType(schema: z.ZodTypeAny): z.ZodTypeAny {\n let current = schema;\n while (true) {\n const typeName = getZodTypeName(current);\n if (!typeName) break;\n const inner = getZodInnerType(current, typeName);\n if (!inner) break;\n current = inner;\n }\n return current;\n}\n"],"mappings":";;;;;;AAWA,SAAgB,UAAU,OAAqC;CAE7D,OACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,WAAW,SACX,OAAQ,MAAc,UAAU,cAChC,eAAe,SACf,OAAQ,MAAc,cAAc;AAExC;;;;;;;;;;;;;;AAeA,SAAgB,eAAe,QAAwC;CACrE,MAAM,YAAY;CAGlB,IAAI,UAAU,MAAM,UAClB,OAAO,UAAU,KAAK;CAIxB,MAAM,WAAW,UAAU,MAAM;CACjC,IAAI,OAAO,aAAa,YAAY,UAElC,OAAO,QAAQ,SAAS,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,SAAS,MAAM,CAAC;AAItE;;;;;;AAOA,SAAgB,WAAW,OAAsC;CAC/D,IAAI,CAAC,UAAU,KAAK,GAAG,OAAO;CAC9B,OAAO,eAAe,KAAmB,MAAM;AACjD;;;;;;AAOA,SAAgB,YAAY,OAAuC;CACjE,IAAI,CAAC,UAAU,KAAK,GAAG,OAAO;CAC9B,OAAO,eAAe,KAAmB,MAAM;A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