zod-compat.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. "use strict";
  2. // zod-compat.ts
  3. // ----------------------------------------------------
  4. // Unified types + helpers to accept Zod v3 and v4 (Mini)
  5. // ----------------------------------------------------
  6. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  7. if (k2 === undefined) k2 = k;
  8. var desc = Object.getOwnPropertyDescriptor(m, k);
  9. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  10. desc = { enumerable: true, get: function() { return m[k]; } };
  11. }
  12. Object.defineProperty(o, k2, desc);
  13. }) : (function(o, m, k, k2) {
  14. if (k2 === undefined) k2 = k;
  15. o[k2] = m[k];
  16. }));
  17. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  18. Object.defineProperty(o, "default", { enumerable: true, value: v });
  19. }) : function(o, v) {
  20. o["default"] = v;
  21. });
  22. var __importStar = (this && this.__importStar) || function (mod) {
  23. if (mod && mod.__esModule) return mod;
  24. var result = {};
  25. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  26. __setModuleDefault(result, mod);
  27. return result;
  28. };
  29. Object.defineProperty(exports, "__esModule", { value: true });
  30. exports.isZ4Schema = isZ4Schema;
  31. exports.objectFromShape = objectFromShape;
  32. exports.safeParse = safeParse;
  33. exports.safeParseAsync = safeParseAsync;
  34. exports.getObjectShape = getObjectShape;
  35. exports.normalizeObjectSchema = normalizeObjectSchema;
  36. exports.getParseErrorMessage = getParseErrorMessage;
  37. exports.getSchemaDescription = getSchemaDescription;
  38. exports.isSchemaOptional = isSchemaOptional;
  39. exports.getLiteralValue = getLiteralValue;
  40. const z3rt = __importStar(require("zod/v3"));
  41. const z4mini = __importStar(require("zod/v4-mini"));
  42. // --- Runtime detection ---
  43. function isZ4Schema(s) {
  44. // Present on Zod 4 (Classic & Mini) schemas; absent on Zod 3
  45. const schema = s;
  46. return !!schema._zod;
  47. }
  48. // --- Schema construction ---
  49. function objectFromShape(shape) {
  50. const values = Object.values(shape);
  51. if (values.length === 0)
  52. return z4mini.object({}); // default to v4 Mini
  53. const allV4 = values.every(isZ4Schema);
  54. const allV3 = values.every(s => !isZ4Schema(s));
  55. if (allV4)
  56. return z4mini.object(shape);
  57. if (allV3)
  58. return z3rt.object(shape);
  59. throw new Error('Mixed Zod versions detected in object shape.');
  60. }
  61. // --- Unified parsing ---
  62. function safeParse(schema, data) {
  63. if (isZ4Schema(schema)) {
  64. // Mini exposes top-level safeParse
  65. const result = z4mini.safeParse(schema, data);
  66. return result;
  67. }
  68. const v3Schema = schema;
  69. const result = v3Schema.safeParse(data);
  70. return result;
  71. }
  72. async function safeParseAsync(schema, data) {
  73. if (isZ4Schema(schema)) {
  74. // Mini exposes top-level safeParseAsync
  75. const result = await z4mini.safeParseAsync(schema, data);
  76. return result;
  77. }
  78. const v3Schema = schema;
  79. const result = await v3Schema.safeParseAsync(data);
  80. return result;
  81. }
  82. // --- Shape extraction ---
  83. function getObjectShape(schema) {
  84. if (!schema)
  85. return undefined;
  86. // Zod v3 exposes `.shape`; Zod v4 keeps the shape on `_zod.def.shape`
  87. let rawShape;
  88. if (isZ4Schema(schema)) {
  89. const v4Schema = schema;
  90. rawShape = v4Schema._zod?.def?.shape;
  91. }
  92. else {
  93. const v3Schema = schema;
  94. rawShape = v3Schema.shape;
  95. }
  96. if (!rawShape)
  97. return undefined;
  98. if (typeof rawShape === 'function') {
  99. try {
  100. return rawShape();
  101. }
  102. catch {
  103. return undefined;
  104. }
  105. }
  106. return rawShape;
  107. }
  108. // --- Schema normalization ---
  109. /**
  110. * Normalizes a schema to an object schema. Handles both:
  111. * - Already-constructed object schemas (v3 or v4)
  112. * - Raw shapes that need to be wrapped into object schemas
  113. */
  114. function normalizeObjectSchema(schema) {
  115. if (!schema)
  116. return undefined;
  117. // First check if it's a raw shape (Record<string, AnySchema>)
  118. // Raw shapes don't have _def or _zod properties and aren't schemas themselves
  119. if (typeof schema === 'object') {
  120. // Check if it's actually a ZodRawShapeCompat (not a schema instance)
  121. // by checking if it lacks schema-like internal properties
  122. const asV3 = schema;
  123. const asV4 = schema;
  124. // If it's not a schema instance (no _def or _zod), it might be a raw shape
  125. if (!asV3._def && !asV4._zod) {
  126. // Check if all values are schemas (heuristic to confirm it's a raw shape)
  127. const values = Object.values(schema);
  128. if (values.length > 0 &&
  129. values.every(v => typeof v === 'object' &&
  130. v !== null &&
  131. (v._def !== undefined ||
  132. v._zod !== undefined ||
  133. typeof v.parse === 'function'))) {
  134. return objectFromShape(schema);
  135. }
  136. }
  137. }
  138. // If we get here, it should be an AnySchema (not a raw shape)
  139. // Check if it's already an object schema
  140. if (isZ4Schema(schema)) {
  141. // Check if it's a v4 object
  142. const v4Schema = schema;
  143. const def = v4Schema._zod?.def;
  144. if (def && (def.type === 'object' || def.shape !== undefined)) {
  145. return schema;
  146. }
  147. }
  148. else {
  149. // Check if it's a v3 object
  150. const v3Schema = schema;
  151. if (v3Schema.shape !== undefined) {
  152. return schema;
  153. }
  154. }
  155. return undefined;
  156. }
  157. // --- Error message extraction ---
  158. /**
  159. * Safely extracts an error message from a parse result error.
  160. * Zod errors can have different structures, so we handle various cases.
  161. */
  162. function getParseErrorMessage(error) {
  163. if (error && typeof error === 'object') {
  164. // Try common error structures
  165. if ('message' in error && typeof error.message === 'string') {
  166. return error.message;
  167. }
  168. if ('issues' in error && Array.isArray(error.issues) && error.issues.length > 0) {
  169. const firstIssue = error.issues[0];
  170. if (firstIssue && typeof firstIssue === 'object' && 'message' in firstIssue) {
  171. return String(firstIssue.message);
  172. }
  173. }
  174. // Fallback: try to stringify the error
  175. try {
  176. return JSON.stringify(error);
  177. }
  178. catch {
  179. return String(error);
  180. }
  181. }
  182. return String(error);
  183. }
  184. // --- Schema metadata access ---
  185. /**
  186. * Gets the description from a schema, if available.
  187. * Works with both Zod v3 and v4.
  188. *
  189. * Both versions expose a `.description` getter that returns the description
  190. * from their respective internal storage (v3: _def, v4: globalRegistry).
  191. */
  192. function getSchemaDescription(schema) {
  193. return schema.description;
  194. }
  195. /**
  196. * Checks if a schema is optional.
  197. * Works with both Zod v3 and v4.
  198. */
  199. function isSchemaOptional(schema) {
  200. if (isZ4Schema(schema)) {
  201. const v4Schema = schema;
  202. return v4Schema._zod?.def?.type === 'optional';
  203. }
  204. const v3Schema = schema;
  205. // v3 has isOptional() method
  206. if (typeof schema.isOptional === 'function') {
  207. return schema.isOptional();
  208. }
  209. return v3Schema._def?.typeName === 'ZodOptional';
  210. }
  211. /**
  212. * Gets the literal value from a schema, if it's a literal schema.
  213. * Works with both Zod v3 and v4.
  214. * Returns undefined if the schema is not a literal or the value cannot be determined.
  215. */
  216. function getLiteralValue(schema) {
  217. if (isZ4Schema(schema)) {
  218. const v4Schema = schema;
  219. const def = v4Schema._zod?.def;
  220. if (def) {
  221. // Try various ways to get the literal value
  222. if (def.value !== undefined)
  223. return def.value;
  224. if (Array.isArray(def.values) && def.values.length > 0) {
  225. return def.values[0];
  226. }
  227. }
  228. }
  229. const v3Schema = schema;
  230. const def = v3Schema._def;
  231. if (def) {
  232. if (def.value !== undefined)
  233. return def.value;
  234. if (Array.isArray(def.values) && def.values.length > 0) {
  235. return def.values[0];
  236. }
  237. }
  238. // Fallback: check for direct value property (some Zod versions)
  239. const directValue = schema.value;
  240. if (directValue !== undefined)
  241. return directValue;
  242. return undefined;
  243. }
  244. //# sourceMappingURL=zod-compat.js.map