@nullix/zod-mongoose-studio 3.3.0 → 3.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.output/nitro.json +1 -1
- package/.output/public/_fonts/57NSSoFy1VLVs2gqly8Ls9awBnZMFyXGrefpmqvdqmc-zJfbBtpgM4cDmcXBsqZNW79_kFnlpPd62b48glgdydA.woff2 +0 -0
- package/.output/public/_fonts/8VR2wSMN-3U4NbWAVYXlkRV6hA0jFBXP-0RtL3X7fko-x2gYI4qfmkRdxyQQUPaBZdZdgl1TeVrquF_TxHeM4lM.woff2 +0 -0
- package/.output/public/_fonts/GsKUclqeNLJ96g5AU593ug6yanivOiwjW_7zESNPChw-jHA4tBeM1bjF7LATGUpfBuSTyomIFrWBTzjF7txVYfg.woff2 +0 -0
- package/.output/public/_fonts/Ld1FnTo3yTIwDyGfTQ5-Fws9AWsCbKfMvgxduXr7JcY-W25bL8NF1fjpLRSOgJb7RoZPHqGQNwMTM7S9tHVoxx8.woff2 +0 -0
- package/.output/public/_fonts/NdzqRASp2bovDUhQT1IRE_EMqKJ2KYQdTCfFcBvL8yw-KhwZiS86o3fErOe5GGMExHUemmI_dBfaEFxjISZrBd0.woff2 +0 -0
- package/.output/public/_fonts/iTkrULNFJJkTvihIg1Vqi5IODRH_9btXCioVF5l98I8-AndUyau2HR2felA_ra8V2mutQgschhasE5FD1dXGJX8.woff2 +0 -0
- package/.output/public/_nuxt/B6lqezyA.js +27 -0
- package/.output/public/_nuxt/{IxYCG8hB.js → B7ScFmDB.js} +1 -1
- package/.output/public/_nuxt/B8gqXD2U.js +1 -0
- package/.output/public/_nuxt/{ikxafTHA.js → BC9tFNFa.js} +180 -184
- package/.output/public/_nuxt/{qwxNOyg7.js → Bbi9UBvi.js} +1 -1
- package/.output/public/_nuxt/{CNZorjDf.js → BcYnCaqL.js} +1 -1
- package/.output/public/_nuxt/{BzhSDy30.js → BlZ0x31M.js} +1 -1
- package/.output/public/_nuxt/Bw82IGM8.js +1 -0
- package/.output/public/_nuxt/{qQyvCkbV.js → C8xmwXC2.js} +1 -1
- package/.output/public/_nuxt/{Ce-T_1aD.js → C919AVYR.js} +1 -1
- package/.output/public/_nuxt/{nDZOJ7rN.js → CsQL5F-l.js} +2 -2
- package/.output/public/_nuxt/Czp4_2o-.js +33 -0
- package/.output/public/_nuxt/{CC3n3Aga.js → DJa7_7Hz.js} +1 -1
- package/.output/public/_nuxt/DKK0Mrrg.js +1 -0
- package/.output/public/_nuxt/DbUZZZZu.js +6 -0
- package/.output/public/_nuxt/{B7P0wQ35.js → DbqvVYbv.js} +1 -1
- package/.output/public/_nuxt/{Ick_23I4.js → JkiDMdYG.js} +1 -1
- package/.output/public/_nuxt/{81vqh61j.js → OVQ3C2Ge.js} +1 -1
- package/.output/public/_nuxt/{Cs8GXJCB.js → PXBhK7S_.js} +1 -1
- package/.output/public/_nuxt/{CAd-qHfI.js → YC-RLggm.js} +1 -1
- package/.output/public/_nuxt/builds/latest.json +1 -1
- package/.output/public/_nuxt/builds/meta/46174b5e-cab6-4a73-95f8-66a1c9b1be2b.json +1 -0
- package/.output/public/_nuxt/{tzj4nvDE.js → d1NBz903.js} +2 -2
- package/.output/public/_nuxt/{entry.J_CbC0oY.css → entry.DaqrxXuH.css} +1 -1
- package/.output/public/_nuxt/error-404.CTSXlFYa.css +1 -0
- package/.output/public/_nuxt/error-500.DqEfQnVZ.css +1 -0
- package/.output/public/_nuxt/{Bd32GOUy.js → iGzUozbF.js} +1 -1
- package/.output/public/_nuxt/{DjPT-NCE.js → iKW_mt0p.js} +1 -1
- package/.output/server/chunks/_/error-500.mjs.map +1 -1
- package/.output/server/chunks/build/client.precomputed.mjs +1 -1
- package/.output/server/chunks/build/{error-404-BN-7rNE4.mjs → error-404-KRkA0DPI.mjs} +10 -4
- package/.output/server/chunks/build/error-404-KRkA0DPI.mjs.map +1 -0
- package/.output/server/chunks/build/error-404-styles.I0NYw-Bn.mjs +8 -0
- package/.output/server/chunks/build/error-404-styles.I0NYw-Bn.mjs.map +1 -0
- package/.output/server/chunks/build/{error-500-DhIQ-2yt.mjs → error-500-DIIFf55p.mjs} +10 -4
- package/.output/server/chunks/build/error-500-DIIFf55p.mjs.map +1 -0
- package/.output/server/chunks/build/error-500-styles.D1cs9hQy.mjs +8 -0
- package/.output/server/chunks/build/error-500-styles.D1cs9hQy.mjs.map +1 -0
- package/.output/server/chunks/build/index-CB1N4gax.mjs +557 -0
- package/.output/server/chunks/build/index-CB1N4gax.mjs.map +1 -0
- package/.output/server/chunks/build/server.mjs +326 -248
- package/.output/server/chunks/build/server.mjs.map +1 -1
- package/.output/server/chunks/build/styles.mjs +4 -4
- package/.output/server/chunks/nitro/nitro.mjs +2319 -852
- package/.output/server/chunks/nitro/nitro.mjs.map +1 -1
- package/.output/server/chunks/routes/api/editor-types.get.mjs +113 -0
- package/.output/server/chunks/routes/api/editor-types.get.mjs.map +1 -0
- package/.output/server/chunks/routes/api/parse.post.mjs +26 -1448
- package/.output/server/chunks/routes/api/parse.post.mjs.map +1 -1
- package/.output/server/chunks/routes/api/resolve.get.mjs +6 -0
- package/.output/server/chunks/routes/api/resolve.get.mjs.map +1 -1
- package/.output/server/chunks/routes/api/studio/run.post.mjs +40 -0
- package/.output/server/chunks/routes/api/studio/run.post.mjs.map +1 -0
- package/.output/server/chunks/routes/renderer.mjs +1 -1
- package/.output/server/chunks/routes/renderer.mjs.map +1 -1
- package/.output/server/index.mjs +7 -1
- package/.output/server/index.mjs.map +1 -1
- package/.output/server/node_modules/acorn/dist/acorn.mjs +6266 -0
- package/.output/server/node_modules/acorn/package.json +50 -0
- package/.output/server/node_modules/ip-address/dist/common.js +112 -4
- package/.output/server/node_modules/ip-address/dist/ipv4.js +322 -87
- package/.output/server/node_modules/ip-address/dist/ipv6.js +654 -199
- package/.output/server/node_modules/ip-address/dist/v4/constants.js +45 -2
- package/.output/server/node_modules/ip-address/dist/v6/constants.js +55 -3
- package/.output/server/node_modules/ip-address/dist/v6/helpers.js +12 -3
- package/.output/server/node_modules/ip-address/package.json +49 -37
- package/.output/server/node_modules/ufo/dist/index.mjs +2 -2
- package/.output/server/node_modules/ufo/package.json +9 -9
- package/.output/server/package.json +4 -3
- package/README.md +65 -20
- package/app/assets/css/main.css +1 -0
- package/app/assets/css/studio.css +131 -0
- package/app/components/StudioConversion.vue +41 -0
- package/app/components/StudioDataPanel.vue +21 -0
- package/app/components/StudioEditor.vue +78 -102
- package/app/components/StudioExpandButton.vue +11 -0
- package/app/components/StudioOutput.vue +7 -1
- package/app/components/StudioSourcePanel.vue +16 -0
- package/app/components/StudioValidation.vue +47 -0
- package/app/components/ZodMongooseStudio.vue +216 -198
- package/app/composables/useStudioRunner.ts +142 -0
- package/app/pages/index.vue +1 -1
- package/app/plugins/monaco.client.ts +2 -0
- package/bin/index.mjs +5 -0
- package/nuxt.config.ts +4 -3
- package/package.json +6 -3
- package/server/api/editor-types.get.ts +7 -0
- package/server/api/parse.post.ts +17 -175
- package/server/api/studio/run.post.ts +29 -0
- package/server/middleware/security.ts +29 -7
- package/server/utils/editor-types.json +1 -0
- package/server/utils/example-data.ts +80 -0
- package/server/utils/parse-request.ts +28 -0
- package/server/utils/runner-client.ts +40 -0
- package/server/utils/studio-execution.ts +176 -0
- package/.output/public/_nuxt/BD40YYqZ.js +0 -33
- package/.output/public/_nuxt/BlQmLjV4.js +0 -1
- package/.output/public/_nuxt/CJ4Cv9fY.js +0 -61
- package/.output/public/_nuxt/builds/meta/495a8ebb-444d-4414-9b98-c181aa84d817.json +0 -1
- package/.output/public/_nuxt/error-404.64pZEiDn.css +0 -1
- package/.output/public/_nuxt/error-500.DHOpI0Ir.css +0 -1
- package/.output/public/_nuxt/rH_O4qXT.js +0 -1
- package/.output/server/chunks/build/error-404-BN-7rNE4.mjs.map +0 -1
- package/.output/server/chunks/build/error-404-styles.Byx46oLR.mjs +0 -8
- package/.output/server/chunks/build/error-404-styles.Byx46oLR.mjs.map +0 -1
- package/.output/server/chunks/build/error-500-DhIQ-2yt.mjs.map +0 -1
- package/.output/server/chunks/build/error-500-styles.BL3UT0Wa.mjs +0 -8
- package/.output/server/chunks/build/error-500-styles.BL3UT0Wa.mjs.map +0 -1
- package/.output/server/chunks/build/index-B4T6zHp0.mjs +0 -1904
- package/.output/server/chunks/build/index-B4T6zHp0.mjs.map +0 -1
- package/.output/server/node_modules/zod/index.js +0 -4
- /package/.output/public/_nuxt/{editor.CdBKkuhR.css → monaco.CdBKkuhR.css} +0 -0
|
@@ -1,1472 +1,50 @@
|
|
|
1
|
-
import
|
|
2
|
-
import vm from 'node:vm';
|
|
3
|
-
import { transformSync } from 'esbuild';
|
|
4
|
-
import util from 'node:util';
|
|
5
|
-
import { fileURLToPath } from 'node:url';
|
|
6
|
-
import { dirname } from 'node:path';
|
|
7
|
-
import * as zod from 'zod';
|
|
8
|
-
import * as mongoose from 'mongoose';
|
|
9
|
-
import { z } from 'zod/v4';
|
|
1
|
+
import { d as defineEventHandler, u as useRuntimeConfig, c as createError, r as readStudioRequest, e as executeStudio, S as StudioExecutionError } from '../../nitro/nitro.mjs';
|
|
10
2
|
import 'node:http';
|
|
3
|
+
import 'node:vm';
|
|
4
|
+
import 'esbuild';
|
|
5
|
+
import 'node:util';
|
|
6
|
+
import 'acorn';
|
|
7
|
+
import 'zod/v4';
|
|
8
|
+
import 'mongoose';
|
|
11
9
|
import 'node:https';
|
|
12
10
|
import 'node:events';
|
|
13
11
|
import 'node:buffer';
|
|
14
12
|
import 'node:fs';
|
|
13
|
+
import 'node:path';
|
|
15
14
|
import 'node:crypto';
|
|
15
|
+
import 'node:url';
|
|
16
16
|
import '@iconify/utils';
|
|
17
17
|
import 'consola';
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
21
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
-
function flatHooks(configHooks, hooks2 = {}, parentName) {
|
|
23
|
-
for (const key in configHooks) {
|
|
24
|
-
const subHook = configHooks[key];
|
|
25
|
-
const name = parentName ? `${parentName}:${key}` : key;
|
|
26
|
-
if (typeof subHook === "object" && subHook !== null) flatHooks(subHook, hooks2, name);
|
|
27
|
-
else if (typeof subHook === "function") hooks2[name] = subHook;
|
|
28
|
-
}
|
|
29
|
-
return hooks2;
|
|
30
|
-
}
|
|
31
|
-
const createTask = /* @__PURE__ */ (() => {
|
|
32
|
-
if (console.createTask) return console.createTask;
|
|
33
|
-
const defaultTask = { run: (fn) => fn() };
|
|
34
|
-
return () => defaultTask;
|
|
35
|
-
})();
|
|
36
|
-
function callHooks(hooks2, args, startIndex, task) {
|
|
37
|
-
for (let i = startIndex; i < hooks2.length; i += 1) try {
|
|
38
|
-
const result = task ? task.run(() => hooks2[i](...args)) : hooks2[i](...args);
|
|
39
|
-
if (result instanceof Promise) return result.then(() => callHooks(hooks2, args, i + 1, task));
|
|
40
|
-
} catch (error) {
|
|
41
|
-
return Promise.reject(error);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function serialTaskCaller(hooks2, args, name) {
|
|
45
|
-
if (hooks2.length > 0) return callHooks(hooks2, args, 0, createTask(name));
|
|
46
|
-
}
|
|
47
|
-
function parallelTaskCaller(hooks2, args, name) {
|
|
48
|
-
if (hooks2.length > 0) {
|
|
49
|
-
const task = createTask(name);
|
|
50
|
-
return Promise.all(hooks2.map((hook) => task.run(() => hook(...args))));
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function callEachWith(callbacks, arg0) {
|
|
54
|
-
for (const callback of [...callbacks]) callback(arg0);
|
|
55
|
-
}
|
|
56
|
-
var Hookable = class {
|
|
57
|
-
constructor() {
|
|
58
|
-
__publicField(this, "_hooks");
|
|
59
|
-
__publicField(this, "_before");
|
|
60
|
-
__publicField(this, "_after");
|
|
61
|
-
__publicField(this, "_deprecatedHooks");
|
|
62
|
-
__publicField(this, "_deprecatedMessages");
|
|
63
|
-
this._hooks = {};
|
|
64
|
-
this._before = void 0;
|
|
65
|
-
this._after = void 0;
|
|
66
|
-
this._deprecatedMessages = void 0;
|
|
67
|
-
this._deprecatedHooks = {};
|
|
68
|
-
this.hook = this.hook.bind(this);
|
|
69
|
-
this.callHook = this.callHook.bind(this);
|
|
70
|
-
this.callHookWith = this.callHookWith.bind(this);
|
|
71
|
-
}
|
|
72
|
-
hook(name, function_, options = {}) {
|
|
73
|
-
if (!name || typeof function_ !== "function") return () => {
|
|
74
|
-
};
|
|
75
|
-
const originalName = name;
|
|
76
|
-
let dep;
|
|
77
|
-
while (this._deprecatedHooks[name]) {
|
|
78
|
-
dep = this._deprecatedHooks[name];
|
|
79
|
-
name = dep.to;
|
|
80
|
-
}
|
|
81
|
-
if (dep && !options.allowDeprecated) {
|
|
82
|
-
let message = dep.message;
|
|
83
|
-
if (!message) message = `${originalName} hook has been deprecated` + (dep.to ? `, please use ${dep.to}` : "");
|
|
84
|
-
if (!this._deprecatedMessages) this._deprecatedMessages = /* @__PURE__ */ new Set();
|
|
85
|
-
if (!this._deprecatedMessages.has(message)) {
|
|
86
|
-
console.warn(message);
|
|
87
|
-
this._deprecatedMessages.add(message);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (!function_.name) try {
|
|
91
|
-
Object.defineProperty(function_, "name", {
|
|
92
|
-
get: () => "_" + name.replace(/\W+/g, "_") + "_hook_cb",
|
|
93
|
-
configurable: true
|
|
94
|
-
});
|
|
95
|
-
} catch {
|
|
96
|
-
}
|
|
97
|
-
this._hooks[name] = this._hooks[name] || [];
|
|
98
|
-
this._hooks[name].push(function_);
|
|
99
|
-
return () => {
|
|
100
|
-
if (function_) {
|
|
101
|
-
this.removeHook(name, function_);
|
|
102
|
-
function_ = void 0;
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
hookOnce(name, function_) {
|
|
107
|
-
let _unreg;
|
|
108
|
-
let _function = (...arguments_) => {
|
|
109
|
-
if (typeof _unreg === "function") _unreg();
|
|
110
|
-
_unreg = void 0;
|
|
111
|
-
_function = void 0;
|
|
112
|
-
return function_(...arguments_);
|
|
113
|
-
};
|
|
114
|
-
_unreg = this.hook(name, _function);
|
|
115
|
-
return _unreg;
|
|
116
|
-
}
|
|
117
|
-
removeHook(name, function_) {
|
|
118
|
-
const hooks2 = this._hooks[name];
|
|
119
|
-
if (hooks2) {
|
|
120
|
-
const index = hooks2.indexOf(function_);
|
|
121
|
-
if (index !== -1) hooks2.splice(index, 1);
|
|
122
|
-
if (hooks2.length === 0) this._hooks[name] = void 0;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
clearHook(name) {
|
|
126
|
-
this._hooks[name] = void 0;
|
|
127
|
-
}
|
|
128
|
-
deprecateHook(name, deprecated) {
|
|
129
|
-
this._deprecatedHooks[name] = typeof deprecated === "string" ? { to: deprecated } : deprecated;
|
|
130
|
-
const _hooks = this._hooks[name] || [];
|
|
131
|
-
this._hooks[name] = void 0;
|
|
132
|
-
for (const hook of _hooks) this.hook(name, hook);
|
|
133
|
-
}
|
|
134
|
-
deprecateHooks(deprecatedHooks) {
|
|
135
|
-
for (const name in deprecatedHooks) this.deprecateHook(name, deprecatedHooks[name]);
|
|
136
|
-
}
|
|
137
|
-
addHooks(configHooks) {
|
|
138
|
-
const hooks2 = flatHooks(configHooks);
|
|
139
|
-
const removeFns = Object.keys(hooks2).map((key) => this.hook(key, hooks2[key]));
|
|
140
|
-
return () => {
|
|
141
|
-
for (const unreg of removeFns) unreg();
|
|
142
|
-
removeFns.length = 0;
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
removeHooks(configHooks) {
|
|
146
|
-
const hooks2 = flatHooks(configHooks);
|
|
147
|
-
for (const key in hooks2) this.removeHook(key, hooks2[key]);
|
|
148
|
-
}
|
|
149
|
-
removeAllHooks() {
|
|
150
|
-
this._hooks = {};
|
|
151
|
-
}
|
|
152
|
-
callHook(name, ...args) {
|
|
153
|
-
return this.callHookWith(serialTaskCaller, name, args);
|
|
154
|
-
}
|
|
155
|
-
callHookParallel(name, ...args) {
|
|
156
|
-
return this.callHookWith(parallelTaskCaller, name, args);
|
|
157
|
-
}
|
|
158
|
-
callHookWith(caller, name, args) {
|
|
159
|
-
const event = this._before || this._after ? {
|
|
160
|
-
name,
|
|
161
|
-
args,
|
|
162
|
-
context: {}
|
|
163
|
-
} : void 0;
|
|
164
|
-
if (this._before) callEachWith(this._before, event);
|
|
165
|
-
const result = caller(this._hooks[name] ? [...this._hooks[name]] : [], args, name);
|
|
166
|
-
if (result instanceof Promise) return result.finally(() => {
|
|
167
|
-
if (this._after && event) callEachWith(this._after, event);
|
|
168
|
-
});
|
|
169
|
-
if (this._after && event) callEachWith(this._after, event);
|
|
170
|
-
return result;
|
|
171
|
-
}
|
|
172
|
-
beforeEach(function_) {
|
|
173
|
-
this._before = this._before || [];
|
|
174
|
-
this._before.push(function_);
|
|
175
|
-
return () => {
|
|
176
|
-
if (this._before !== void 0) {
|
|
177
|
-
const index = this._before.indexOf(function_);
|
|
178
|
-
if (index !== -1) this._before.splice(index, 1);
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
afterEach(function_) {
|
|
183
|
-
this._after = this._after || [];
|
|
184
|
-
this._after.push(function_);
|
|
185
|
-
return () => {
|
|
186
|
-
if (this._after !== void 0) {
|
|
187
|
-
const index = this._after.indexOf(function_);
|
|
188
|
-
if (index !== -1) this._after.splice(index, 1);
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
function createHooks() {
|
|
194
|
-
return new Hookable();
|
|
195
|
-
}
|
|
196
|
-
const hooks = createHooks();
|
|
197
|
-
function callHookSync(name, ...args) {
|
|
198
|
-
hooks.callHookWith((callbacks, args2) => {
|
|
199
|
-
for (const callback of callbacks) {
|
|
200
|
-
callback(...args2);
|
|
201
|
-
}
|
|
202
|
-
}, name, args);
|
|
203
|
-
}
|
|
204
|
-
const mongooseRegistry = z.registry();
|
|
205
|
-
function withMongoose(schema, meta = {}) {
|
|
206
|
-
callHookSync("registry:get:before", { schema });
|
|
207
|
-
const existing = mongooseRegistry.get(schema) || {};
|
|
208
|
-
callHookSync("registry:get", { schema, meta: existing });
|
|
209
|
-
const merged = { ...existing, ...meta };
|
|
210
|
-
callHookSync("registry:add", { schema, meta: merged });
|
|
211
|
-
mongooseRegistry.add(schema, merged);
|
|
212
|
-
callHookSync("registry:added", { schema, meta: merged });
|
|
213
|
-
return schema;
|
|
214
|
-
}
|
|
215
|
-
function getMongooseMeta(schema) {
|
|
216
|
-
const def = schema._def;
|
|
217
|
-
if (!def)
|
|
218
|
-
return {};
|
|
219
|
-
let meta = mongooseRegistry.get(schema) || {};
|
|
220
|
-
if (def.innerType) {
|
|
221
|
-
meta = { ...getMongooseMeta(def.innerType), ...meta };
|
|
222
|
-
} else if (def.schema) {
|
|
223
|
-
meta = { ...getMongooseMeta(def.schema), ...meta };
|
|
224
|
-
}
|
|
225
|
-
if (def.type === "pipe") {
|
|
226
|
-
meta = { ...getMongooseMeta(def.in), ...getMongooseMeta(def.out), ...meta };
|
|
227
|
-
}
|
|
228
|
-
return meta;
|
|
229
|
-
}
|
|
230
|
-
function objectStrictness(schema) {
|
|
231
|
-
var _a, _b, _c, _d;
|
|
232
|
-
const catchall = (_b = (_a = schema._zod) == null ? void 0 : _a.def) == null ? void 0 : _b.catchall;
|
|
233
|
-
if (!catchall)
|
|
234
|
-
return true;
|
|
235
|
-
return ((_d = (_c = catchall._zod) == null ? void 0 : _c.def) == null ? void 0 : _d.type) === "never" ? "throw" : false;
|
|
236
|
-
}
|
|
237
|
-
function unwrapZodSchema(schema, features = { required: true }, visited = /* @__PURE__ */ new Set()) {
|
|
238
|
-
var _a, _b, _c, _d, _e;
|
|
239
|
-
if (!schema)
|
|
240
|
-
return { schema, features };
|
|
241
|
-
if (visited.has(schema))
|
|
242
|
-
return { schema, features };
|
|
243
|
-
const def = schema._def;
|
|
244
|
-
if (!def)
|
|
245
|
-
return { schema, features };
|
|
246
|
-
if (!(schema instanceof z.ZodOptional) && !(schema instanceof z.ZodNullable) && !(schema instanceof z.ZodDefault) && def.type !== "pipe") {
|
|
247
|
-
visited.add(schema);
|
|
248
|
-
}
|
|
249
|
-
if (schema instanceof z.ZodOptional) {
|
|
250
|
-
const inner = schema.unwrap();
|
|
251
|
-
const result = unwrapZodSchema(
|
|
252
|
-
// @ts-expect-error Zod v4 schema.unwrap() return type mismatch
|
|
253
|
-
inner,
|
|
254
|
-
features,
|
|
255
|
-
visited
|
|
256
|
-
);
|
|
257
|
-
return {
|
|
258
|
-
schema: result.schema,
|
|
259
|
-
features: {
|
|
260
|
-
...result.features,
|
|
261
|
-
required: false,
|
|
262
|
-
isOptional: true,
|
|
263
|
-
isExactOptional: false,
|
|
264
|
-
isNonOptional: false
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
if (schema instanceof z.ZodExactOptional) {
|
|
269
|
-
const result = unwrapZodSchema(schema.unwrap(), features, visited);
|
|
270
|
-
return {
|
|
271
|
-
schema: result.schema,
|
|
272
|
-
features: {
|
|
273
|
-
...result.features,
|
|
274
|
-
required: false,
|
|
275
|
-
isOptional: true,
|
|
276
|
-
isExactOptional: true,
|
|
277
|
-
isNonOptional: false
|
|
278
|
-
}
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
if (schema instanceof z.ZodNonOptional) {
|
|
282
|
-
const result = unwrapZodSchema(schema.unwrap(), features, visited);
|
|
283
|
-
return {
|
|
284
|
-
schema: result.schema,
|
|
285
|
-
features: {
|
|
286
|
-
...result.features,
|
|
287
|
-
required: true,
|
|
288
|
-
isOptional: false,
|
|
289
|
-
isExactOptional: false,
|
|
290
|
-
isNonOptional: true
|
|
291
|
-
}
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
if (schema instanceof z.ZodNullable) {
|
|
295
|
-
return unwrapZodSchema(
|
|
296
|
-
// @ts-expect-error Zod v4 schema.unwrap() return type mismatch
|
|
297
|
-
schema.unwrap(),
|
|
298
|
-
{
|
|
299
|
-
...features,
|
|
300
|
-
isNullable: true
|
|
301
|
-
},
|
|
302
|
-
visited
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
if (schema instanceof z.ZodDefault) {
|
|
306
|
-
const defaultValue = typeof def.defaultValue === "function" ? def.defaultValue() : def.defaultValue;
|
|
307
|
-
return unwrapZodSchema(def.innerType, {
|
|
308
|
-
...features,
|
|
309
|
-
default: defaultValue
|
|
310
|
-
}, visited);
|
|
311
|
-
}
|
|
312
|
-
const { type } = def;
|
|
313
|
-
if (type === "pipe") {
|
|
314
|
-
const inType = (_b = (_a = def.in) == null ? void 0 : _a._def) == null ? void 0 : _b.type;
|
|
315
|
-
const outType = (_d = (_c = def.out) == null ? void 0 : _c._def) == null ? void 0 : _d.type;
|
|
316
|
-
if (inType === "transform") {
|
|
317
|
-
return unwrapZodSchema(def.out, features, visited);
|
|
318
|
-
}
|
|
319
|
-
if (outType === "transform" || outType === "refinement") {
|
|
320
|
-
const transformFeatures = { ...features };
|
|
321
|
-
const outDef = (_e = def.out) == null ? void 0 : _e._def;
|
|
322
|
-
const effects = (outDef == null ? void 0 : outDef.effects) || (outDef == null ? void 0 : outDef.transformations) || (outType === "transform" ? [outDef] : []);
|
|
323
|
-
if (effects && Array.isArray(effects)) {
|
|
324
|
-
transformFeatures.transformations = [
|
|
325
|
-
...transformFeatures.transformations || [],
|
|
326
|
-
...effects
|
|
327
|
-
];
|
|
328
|
-
}
|
|
329
|
-
return unwrapZodSchema(def.in, transformFeatures, visited);
|
|
330
|
-
}
|
|
331
|
-
return unwrapZodSchema(def.out, features, visited);
|
|
332
|
-
}
|
|
333
|
-
if (type === "transform" || type === "preprocess" || type === "refinement" || type === "effects") {
|
|
334
|
-
const transformFeatures = { ...features };
|
|
335
|
-
const effects = def.effects || def.transformations || (def.type === "transform" || type === "transform" ? [def] : []);
|
|
336
|
-
if (effects && Array.isArray(effects)) {
|
|
337
|
-
transformFeatures.transformations = [
|
|
338
|
-
...transformFeatures.transformations || [],
|
|
339
|
-
...effects
|
|
340
|
-
];
|
|
341
|
-
}
|
|
342
|
-
const inner = def.schema || def.innerType;
|
|
343
|
-
if (inner) {
|
|
344
|
-
const result = unwrapZodSchema(inner, transformFeatures, visited);
|
|
345
|
-
return result;
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
if (type === "lazy") {
|
|
349
|
-
return { schema, features };
|
|
350
|
-
}
|
|
351
|
-
if (type === "branded" || type === "readonly") {
|
|
352
|
-
return unwrapZodSchema(schema.unwrap(), {
|
|
353
|
-
...features,
|
|
354
|
-
...type === "readonly" ? { readOnly: true } : {}
|
|
355
|
-
}, visited);
|
|
356
|
-
}
|
|
357
|
-
if (def.checks && Array.isArray(def.checks)) {
|
|
358
|
-
features.checks = [...features.checks || [], ...def.checks];
|
|
359
|
-
}
|
|
360
|
-
return { schema, features };
|
|
361
|
-
}
|
|
362
|
-
function prepareForZodValidation(schema, value, mongooseSchema, pathPrefix = "") {
|
|
363
|
-
var _a;
|
|
364
|
-
if (value === null || value === void 0)
|
|
365
|
-
return value;
|
|
366
|
-
const { schema: unwrapped } = unwrapZodSchema(schema);
|
|
367
|
-
const def = unwrapped == null ? void 0 : unwrapped._def;
|
|
368
|
-
if ((def == null ? void 0 : def.type) === "lazy") {
|
|
369
|
-
return prepareForZodValidation(def.getter(), value, mongooseSchema, pathPrefix);
|
|
370
|
-
}
|
|
371
|
-
if ((def == null ? void 0 : def.type) === "object" && typeof value === "object" && !Array.isArray(value)) {
|
|
372
|
-
const { shape } = unwrapped;
|
|
373
|
-
const result = { ...value };
|
|
374
|
-
if (mongooseSchema && !pathPrefix && mongooseSchema.options._id !== false && !Object.hasOwn(shape, "_id")) {
|
|
375
|
-
delete result._id;
|
|
376
|
-
}
|
|
377
|
-
const versionKey = !pathPrefix && (mongooseSchema == null ? void 0 : mongooseSchema.options.versionKey);
|
|
378
|
-
if (typeof versionKey === "string" && !Object.hasOwn(shape, versionKey)) {
|
|
379
|
-
delete result[versionKey];
|
|
380
|
-
}
|
|
381
|
-
const timestamps = !pathPrefix && (mongooseSchema == null ? void 0 : mongooseSchema.options.timestamps);
|
|
382
|
-
if (timestamps) {
|
|
383
|
-
for (const key of ["createdAt", "updatedAt"]) {
|
|
384
|
-
const configured = typeof timestamps === "object" ? timestamps[key] : void 0;
|
|
385
|
-
if (configured === false)
|
|
386
|
-
continue;
|
|
387
|
-
const fieldName = typeof configured === "string" ? configured : key;
|
|
388
|
-
if (!Object.hasOwn(shape, fieldName))
|
|
389
|
-
delete result[fieldName];
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
for (const [key, fieldSchema] of Object.entries(shape)) {
|
|
393
|
-
if (!Object.hasOwn(result, key))
|
|
394
|
-
continue;
|
|
395
|
-
const path = pathPrefix ? `${pathPrefix}.${key}` : key;
|
|
396
|
-
const nestedSchema = (_a = mongooseSchema == null ? void 0 : mongooseSchema.path(path)) == null ? void 0 : _a.schema;
|
|
397
|
-
result[key] = prepareForZodValidation(fieldSchema, result[key], nestedSchema || mongooseSchema, nestedSchema ? "" : path);
|
|
398
|
-
}
|
|
399
|
-
return result;
|
|
400
|
-
}
|
|
401
|
-
if (Array.isArray(value)) {
|
|
402
|
-
if ((def == null ? void 0 : def.type) === "array" || (def == null ? void 0 : def.type) === "set") {
|
|
403
|
-
const element = unwrapped.element || def.valueType;
|
|
404
|
-
return element ? value.map((item) => prepareForZodValidation(element, item, mongooseSchema, pathPrefix)) : value;
|
|
405
|
-
}
|
|
406
|
-
if ((def == null ? void 0 : def.type) === "tuple") {
|
|
407
|
-
return value.map((item, index) => {
|
|
408
|
-
var _a2;
|
|
409
|
-
const element = ((_a2 = def.items) == null ? void 0 : _a2[index]) || def.rest;
|
|
410
|
-
return element ? prepareForZodValidation(element, item, mongooseSchema, pathPrefix) : item;
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
return value;
|
|
415
|
-
}
|
|
416
|
-
let mongooseInstance = null;
|
|
417
|
-
let isFrontend;
|
|
418
|
-
const setFrontendMode = (enabled) => {
|
|
419
|
-
console.warn("[zod-mongoose] setFrontendMode() is deprecated and will be removed in v4. Conditional exports select the appropriate implementation automatically.");
|
|
420
|
-
isFrontend = enabled;
|
|
421
|
-
};
|
|
422
|
-
const getFrontendMode = () => {
|
|
423
|
-
if (isFrontend === void 0 || isFrontend === null) {
|
|
424
|
-
return globalThis.window !== void 0 && globalThis.document !== void 0;
|
|
425
|
-
}
|
|
426
|
-
return isFrontend;
|
|
427
|
-
};
|
|
428
|
-
const setMongoose = (m) => {
|
|
429
|
-
mongooseInstance = m;
|
|
430
|
-
};
|
|
431
|
-
const getMongoose = () => {
|
|
432
|
-
var _a;
|
|
433
|
-
if (getFrontendMode()) {
|
|
434
|
-
return null;
|
|
435
|
-
}
|
|
436
|
-
if (mongooseInstance) {
|
|
437
|
-
return mongooseInstance;
|
|
438
|
-
}
|
|
439
|
-
try {
|
|
440
|
-
if (typeof require !== "undefined") {
|
|
441
|
-
const m = require("mongoose");
|
|
442
|
-
if (m && (m.Schema || ((_a = m.default) == null ? void 0 : _a.Schema))) {
|
|
443
|
-
return m.default || m;
|
|
444
|
-
}
|
|
445
|
-
return m;
|
|
446
|
-
}
|
|
447
|
-
return null;
|
|
448
|
-
} catch {
|
|
449
|
-
if (globalThis.mongoose) {
|
|
450
|
-
return globalThis.mongoose;
|
|
451
|
-
}
|
|
452
|
-
return null;
|
|
453
|
-
}
|
|
454
|
-
};
|
|
455
|
-
function mapZodChecksToMongoose(checks, mongooseProp) {
|
|
456
|
-
var _a, _b;
|
|
457
|
-
if (!checks || !Array.isArray(checks))
|
|
458
|
-
return;
|
|
459
|
-
for (const check of checks) {
|
|
460
|
-
const traitSet = (_a = check._zod) == null ? void 0 : _a.traits;
|
|
461
|
-
const checkDef = (_b = check._zod) == null ? void 0 : _b.def;
|
|
462
|
-
if (!traitSet || !checkDef)
|
|
463
|
-
continue;
|
|
464
|
-
if (traitSet.has("$ZodCheckMinLength")) {
|
|
465
|
-
mongooseProp.minlength = checkDef.minimum;
|
|
466
|
-
}
|
|
467
|
-
if (traitSet.has("$ZodCheckMaxLength")) {
|
|
468
|
-
mongooseProp.maxlength = checkDef.maximum;
|
|
469
|
-
}
|
|
470
|
-
if (traitSet.has("$ZodCheckLengthEquals")) {
|
|
471
|
-
mongooseProp.minlength = checkDef.length;
|
|
472
|
-
mongooseProp.maxlength = checkDef.length;
|
|
473
|
-
}
|
|
474
|
-
if (traitSet.has("$ZodCheckGreaterThan")) {
|
|
475
|
-
mongooseProp.min = checkDef.value;
|
|
476
|
-
}
|
|
477
|
-
if (traitSet.has("$ZodCheckLessThan")) {
|
|
478
|
-
mongooseProp.max = checkDef.value;
|
|
479
|
-
}
|
|
480
|
-
if (traitSet.has("$ZodCheckRegex")) {
|
|
481
|
-
mongooseProp.match = checkDef.pattern;
|
|
482
|
-
}
|
|
483
|
-
if (traitSet.has("$ZodUUID")) {
|
|
484
|
-
const mongoose = globalThis.mongoose || globalThis.__mongoose;
|
|
485
|
-
if (mongoose == null ? void 0 : mongoose.Schema.Types.UUID) {
|
|
486
|
-
mongooseProp.type = mongoose.Schema.Types.UUID;
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
if (traitSet.has("$ZodISODateTime") || traitSet.has("$ZodISODate")) {
|
|
490
|
-
mongooseProp.type = Date;
|
|
491
|
-
}
|
|
492
|
-
if (traitSet.has("$ZodCheckOverwrite") && typeof checkDef.tx === "function") {
|
|
493
|
-
const txStr = checkDef.tx.toString();
|
|
494
|
-
if (txStr.includes(".trim()")) {
|
|
495
|
-
mongooseProp.trim = true;
|
|
496
|
-
} else if (txStr.includes(".toLowerCase()")) {
|
|
497
|
-
mongooseProp.lowercase = true;
|
|
498
|
-
} else if (txStr.includes(".toUpperCase()")) {
|
|
499
|
-
mongooseProp.uppercase = true;
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
callHookSync("validation:mappers", { checks, mongooseProp });
|
|
504
|
-
}
|
|
505
|
-
function handleObject(unwrapped, mongooseProp, visited, extractMongooseDef2, isField = false) {
|
|
506
|
-
var _a, _b, _c, _d, _e;
|
|
507
|
-
callHookSync("schema:object:before", { schema: unwrapped, mongooseProp, visited });
|
|
508
|
-
const { shape } = unwrapped;
|
|
509
|
-
const objDef = {};
|
|
510
|
-
const placeholder = mongooseProp.type ? mongooseProp : objDef;
|
|
511
|
-
visited.set(unwrapped, placeholder);
|
|
512
|
-
for (const key in shape) {
|
|
513
|
-
if (!Object.prototype.hasOwnProperty.call(shape, key))
|
|
514
|
-
continue;
|
|
515
|
-
if (key === "_id") {
|
|
516
|
-
const idMeta = mongooseRegistry.get(shape[key]) || {};
|
|
517
|
-
const unwrappedId = unwrapZodSchema(shape[key]).schema;
|
|
518
|
-
const unwrappedIdMeta = mongooseRegistry.get(unwrappedId) || {};
|
|
519
|
-
if (idMeta.includeId !== true && unwrappedIdMeta.includeId !== true && mongooseProp.includeId !== true) {
|
|
520
|
-
continue;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
const def = extractMongooseDef2(shape[key], visited);
|
|
524
|
-
if (def && typeof def === "object" && def.__isDiscriminatorUnion) {
|
|
525
|
-
const mongoose = getMongoose();
|
|
526
|
-
if (mongoose) {
|
|
527
|
-
const baseSchema = new mongoose.Schema(def.baseDef, {
|
|
528
|
-
discriminatorKey: def.discriminatorKey,
|
|
529
|
-
_id: false
|
|
530
|
-
});
|
|
531
|
-
const discriminators = {};
|
|
532
|
-
for (const [dKey, dDef] of Object.entries(def.discriminators)) {
|
|
533
|
-
discriminators[dKey] = new mongoose.Schema(dDef, { _id: false });
|
|
534
|
-
}
|
|
535
|
-
objDef[key] = {
|
|
536
|
-
type: baseSchema,
|
|
537
|
-
discriminators
|
|
538
|
-
};
|
|
539
|
-
} else {
|
|
540
|
-
objDef[key] = def;
|
|
541
|
-
}
|
|
542
|
-
} else if (typeof def === "object" && def !== null && !Array.isArray(def)) {
|
|
543
|
-
const { includeId, ...cleanDef } = def;
|
|
544
|
-
objDef[key] = cleanDef;
|
|
545
|
-
} else {
|
|
546
|
-
objDef[key] = def;
|
|
547
|
-
}
|
|
548
|
-
callHookSync("schema:object:field", { key, schema: shape[key], objDef, visited });
|
|
549
|
-
}
|
|
550
|
-
let result;
|
|
551
|
-
const shouldBeSubSchema = isField && mongooseProp.schema !== false;
|
|
552
|
-
if (isField && mongooseProp.schema === false && !mongooseProp.type && objectStrictness(unwrapped) !== true) {
|
|
553
|
-
throw new Error("A strict, loose, or catchall Zod object needs a Mongoose subschema. Remove {schema: false} or provide a Mongoose type override.");
|
|
554
|
-
}
|
|
555
|
-
if (shouldBeSubSchema && !mongooseProp.type) {
|
|
556
|
-
const mongoose = getMongoose();
|
|
557
|
-
if (mongoose) {
|
|
558
|
-
const options = typeof mongooseProp.schema === "object" ? mongooseProp.schema : {};
|
|
559
|
-
const { plugins, ...schemaOptions } = options;
|
|
560
|
-
const strict = (_b = (_a = schemaOptions.strict) != null ? _a : mongooseProp.strict) != null ? _b : objectStrictness(unwrapped);
|
|
561
|
-
const minimize = (_d = (_c = schemaOptions.minimize) != null ? _c : mongooseProp.minimize) != null ? _d : objectStrictness(unwrapped) === false ? false : void 0;
|
|
562
|
-
const subSchema = new mongoose.Schema(objDef, {
|
|
563
|
-
...schemaOptions,
|
|
564
|
-
strict,
|
|
565
|
-
...minimize === void 0 ? {} : { minimize }
|
|
566
|
-
});
|
|
567
|
-
if (plugins && Array.isArray(plugins)) {
|
|
568
|
-
for (const plugin of plugins) {
|
|
569
|
-
subSchema.plugin(plugin);
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
mongooseProp.type = subSchema;
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
if (mongooseProp.type) {
|
|
576
|
-
const mongoose = getMongoose();
|
|
577
|
-
const isSchema = mongoose && (mongooseProp.type instanceof mongoose.Schema || ((_e = mongooseProp.type.constructor) == null ? void 0 : _e.name) === "Schema");
|
|
578
|
-
if (!isSchema) {
|
|
579
|
-
Object.assign(mongooseProp, objDef);
|
|
580
|
-
}
|
|
581
|
-
result = mongooseProp;
|
|
582
|
-
} else {
|
|
583
|
-
Object.assign(mongooseProp, objDef);
|
|
584
|
-
const topLevelOptions = /* @__PURE__ */ new Set([
|
|
585
|
-
"collection",
|
|
586
|
-
"versionKey",
|
|
587
|
-
"timestamps",
|
|
588
|
-
"discriminatorKey",
|
|
589
|
-
"strict",
|
|
590
|
-
"id",
|
|
591
|
-
"_id",
|
|
592
|
-
"minimize",
|
|
593
|
-
"validateBeforeSave",
|
|
594
|
-
"schema"
|
|
595
|
-
]);
|
|
596
|
-
const hasFieldMetadata = Object.keys(mongooseProp).some((k) => {
|
|
597
|
-
if (Object.prototype.hasOwnProperty.call(objDef, k))
|
|
598
|
-
return false;
|
|
599
|
-
if (topLevelOptions.has(k))
|
|
600
|
-
return false;
|
|
601
|
-
if (k === "required" && mongooseProp[k] === false)
|
|
602
|
-
return false;
|
|
603
|
-
return true;
|
|
604
|
-
});
|
|
605
|
-
result = hasFieldMetadata ? mongooseProp : objDef;
|
|
606
|
-
}
|
|
607
|
-
callHookSync("schema:object:after", { schema: unwrapped, mongooseProp, objDef, result });
|
|
608
|
-
return result;
|
|
609
|
-
}
|
|
610
|
-
function handleArray(unwrapped, mongooseProp, visited, extractMongooseDef2) {
|
|
611
|
-
var _a;
|
|
612
|
-
callHookSync("schema:array:before", { schema: unwrapped, mongooseProp, visited });
|
|
613
|
-
const element = unwrapped.element || unwrapped._def.valueType || unwrapped._def.rest || ((_a = unwrapped._def.items) == null ? void 0 : _a[0]);
|
|
614
|
-
const mongoose = getMongoose();
|
|
615
|
-
const innerDef = element ? extractMongooseDef2(element, visited) : (mongoose == null ? void 0 : mongoose.Schema.Types.Mixed) || "Mixed";
|
|
616
|
-
if (!mongooseProp.type) {
|
|
617
|
-
if (innerDef && typeof innerDef === "object" && innerDef.__isDiscriminatorUnion && mongoose) {
|
|
618
|
-
const discriminators = {};
|
|
619
|
-
for (const [dKey, dDef] of Object.entries(innerDef.discriminators)) {
|
|
620
|
-
discriminators[dKey] = new mongoose.Schema(dDef, { _id: false });
|
|
621
|
-
}
|
|
622
|
-
mongooseProp.type = [
|
|
623
|
-
new mongoose.Schema({}, {
|
|
624
|
-
discriminatorKey: innerDef.discriminatorKey,
|
|
625
|
-
_id: false
|
|
626
|
-
})
|
|
627
|
-
];
|
|
628
|
-
mongooseProp.discriminators = discriminators;
|
|
629
|
-
} else {
|
|
630
|
-
const innerType = innerDef.type || innerDef;
|
|
631
|
-
mongooseProp.type = [innerType];
|
|
632
|
-
if (typeof innerDef === "object") {
|
|
633
|
-
const { type: _extractedType, ...innerMeta } = innerDef;
|
|
634
|
-
Object.assign(mongooseProp, innerMeta);
|
|
635
|
-
mongooseProp.type = [innerType];
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
callHookSync("schema:array:after", { schema: unwrapped, mongooseProp, innerDef });
|
|
640
|
-
}
|
|
641
|
-
function handleRecord(unwrapped, mongooseProp, visited, extractMongooseDef2) {
|
|
642
|
-
var _a, _b;
|
|
643
|
-
callHookSync("schema:record:before", { schema: unwrapped, mongooseProp, visited });
|
|
644
|
-
const type = (_a = unwrapped._def) == null ? void 0 : _a.type;
|
|
645
|
-
const isMap = type === "map";
|
|
646
|
-
const valueType = unwrapped.valueType || unwrapped.valueSchema || unwrapped._def.valueType || unwrapped._def.valueSchema || unwrapped._def.innerType;
|
|
647
|
-
let innerDef;
|
|
648
|
-
if (!mongooseProp.type || mongooseProp.type === Map || mongooseProp.type === Object) {
|
|
649
|
-
mongooseProp.type = isMap ? Map : Object;
|
|
650
|
-
const finalValueType = valueType || unwrapped.valueSchema || ((_b = unwrapped._def) == null ? void 0 : _b.valueSchema);
|
|
651
|
-
if (finalValueType) {
|
|
652
|
-
innerDef = extractMongooseDef2(finalValueType, visited);
|
|
653
|
-
mongooseProp.of = innerDef.type || innerDef;
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
callHookSync("schema:record:after", { schema: unwrapped, mongooseProp, innerDef });
|
|
657
|
-
}
|
|
658
|
-
function areDefinitionsEqual(left, right, seen = /* @__PURE__ */ new WeakMap()) {
|
|
659
|
-
if (left === right)
|
|
660
|
-
return true;
|
|
661
|
-
if (left === null || right === null || typeof left !== "object" || typeof right !== "object") {
|
|
662
|
-
return false;
|
|
663
|
-
}
|
|
664
|
-
const mongoose = getMongoose();
|
|
665
|
-
const leftIsSchema = mongoose && left instanceof mongoose.Schema;
|
|
666
|
-
const rightIsSchema = mongoose && right instanceof mongoose.Schema;
|
|
667
|
-
if (leftIsSchema || rightIsSchema) {
|
|
668
|
-
return Boolean(leftIsSchema && rightIsSchema && areDefinitionsEqual(left.obj, right.obj, seen));
|
|
669
|
-
}
|
|
670
|
-
let seenRight = seen.get(left);
|
|
671
|
-
if (seenRight == null ? void 0 : seenRight.has(right))
|
|
672
|
-
return true;
|
|
673
|
-
if (!seenRight) {
|
|
674
|
-
seenRight = /* @__PURE__ */ new WeakSet();
|
|
675
|
-
seen.set(left, seenRight);
|
|
676
|
-
}
|
|
677
|
-
seenRight.add(right);
|
|
678
|
-
if (Array.isArray(left) || Array.isArray(right)) {
|
|
679
|
-
return Array.isArray(left) && Array.isArray(right) && left.length === right.length && left.every((value, index) => areDefinitionsEqual(value, right[index], seen));
|
|
680
|
-
}
|
|
681
|
-
const leftKeys = Object.keys(left);
|
|
682
|
-
const rightKeys = Object.keys(right);
|
|
683
|
-
return leftKeys.length === rightKeys.length && leftKeys.every((key) => Object.prototype.hasOwnProperty.call(right, key) && areDefinitionsEqual(left[key], right[key], seen));
|
|
684
|
-
}
|
|
685
|
-
function extractMongooseDef(schema, visited = /* @__PURE__ */ new Map(), isField = false, noWrap = false) {
|
|
686
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
687
|
-
if (visited.size === 0) {
|
|
688
|
-
callHookSync("converter:before", { schema, visited });
|
|
689
|
-
}
|
|
690
|
-
callHookSync("converter:start", { schema, visited });
|
|
691
|
-
const { schema: unwrapped, features } = unwrapZodSchema(schema);
|
|
692
|
-
const meta = mongooseRegistry.get(schema) || {};
|
|
693
|
-
const mongooseProp = getMongooseMeta(schema);
|
|
694
|
-
callHookSync("converter:unwrapped", {
|
|
695
|
-
schema,
|
|
696
|
-
unwrapped,
|
|
697
|
-
features,
|
|
698
|
-
meta: mongooseProp,
|
|
699
|
-
mongooseProp
|
|
700
|
-
});
|
|
701
|
-
if (features.isOptional === true && mongooseProp.type && mongooseProp.required !== true) {
|
|
702
|
-
mongooseProp.required = false;
|
|
703
|
-
}
|
|
704
|
-
if (visited.has(schema)) {
|
|
705
|
-
const existing = visited.get(schema);
|
|
706
|
-
if (existing === mongooseProp) {
|
|
707
|
-
return existing;
|
|
708
|
-
}
|
|
709
|
-
if (Object.keys(meta).length > 0) {
|
|
710
|
-
Object.assign(existing, mongooseProp);
|
|
711
|
-
}
|
|
712
|
-
return existing;
|
|
713
|
-
}
|
|
714
|
-
visited.set(schema, mongooseProp);
|
|
715
|
-
if (features.default !== void 0) {
|
|
716
|
-
mongooseProp.default = features.default;
|
|
717
|
-
}
|
|
718
|
-
if (features.required === false) {
|
|
719
|
-
mongooseProp.required = false;
|
|
720
|
-
}
|
|
721
|
-
if (features.isNullable === true && mongooseProp.required === void 0) {
|
|
722
|
-
mongooseProp.required = false;
|
|
723
|
-
}
|
|
724
|
-
if (features.isNonOptional === true && features.isNullable !== true && mongooseProp.required !== false) {
|
|
725
|
-
mongooseProp.required = true;
|
|
726
|
-
}
|
|
727
|
-
if (features.readOnly === true) {
|
|
728
|
-
mongooseProp.readOnly = true;
|
|
729
|
-
}
|
|
730
|
-
if (isField && features.isExactOptional === true) {
|
|
731
|
-
const previousSetter = mongooseProp.set;
|
|
732
|
-
mongooseProp.set = function exactOptionalSetter(value) {
|
|
733
|
-
if (value === void 0) {
|
|
734
|
-
throw new TypeError("An exactOptional field cannot be set to undefined");
|
|
735
|
-
}
|
|
736
|
-
return typeof previousSetter === "function" ? previousSetter.call(this, value) : value;
|
|
737
|
-
};
|
|
738
|
-
}
|
|
739
|
-
mapZodChecksToMongoose(features.checks, mongooseProp);
|
|
740
|
-
const def = unwrapped._def;
|
|
741
|
-
if (!def) {
|
|
742
|
-
callHookSync("converter:after", {
|
|
743
|
-
schema,
|
|
744
|
-
mongooseProp
|
|
745
|
-
});
|
|
746
|
-
return mongooseProp;
|
|
747
|
-
}
|
|
748
|
-
const { type } = def;
|
|
749
|
-
callHookSync("converter:node", {
|
|
750
|
-
schema: unwrapped,
|
|
751
|
-
mongooseProp,
|
|
752
|
-
type
|
|
753
|
-
});
|
|
754
|
-
if (type === "object") {
|
|
755
|
-
const wrapperFn = (s, v) => extractMongooseDef(s, v, true);
|
|
756
|
-
const previousUnwrapped = schema === unwrapped ? void 0 : visited.get(unwrapped);
|
|
757
|
-
const result = handleObject(unwrapped, mongooseProp, visited, wrapperFn, isField && !noWrap);
|
|
758
|
-
visited.set(schema, result);
|
|
759
|
-
if (schema !== unwrapped) {
|
|
760
|
-
if (previousUnwrapped === void 0)
|
|
761
|
-
visited.delete(unwrapped);
|
|
762
|
-
else
|
|
763
|
-
visited.set(unwrapped, previousUnwrapped);
|
|
764
|
-
}
|
|
765
|
-
callHookSync("converter:after", {
|
|
766
|
-
schema,
|
|
767
|
-
mongooseProp: result
|
|
768
|
-
});
|
|
769
|
-
if (typeof result === "object" && result !== null && !Array.isArray(result)) {
|
|
770
|
-
delete result.includeId;
|
|
771
|
-
}
|
|
772
|
-
return result;
|
|
773
|
-
}
|
|
774
|
-
if (type === "array" || type === "set" || type === "tuple") {
|
|
775
|
-
handleArray(unwrapped, mongooseProp, visited, (s, v) => extractMongooseDef(s, v, true));
|
|
776
|
-
}
|
|
777
|
-
if (type === "record" || type === "map") {
|
|
778
|
-
handleRecord(unwrapped, mongooseProp, visited, (s, v) => extractMongooseDef(s, v, true));
|
|
779
|
-
}
|
|
780
|
-
if (type === "intersection") {
|
|
781
|
-
const left = extractMongooseDef(unwrapped._def.left, visited, isField, true);
|
|
782
|
-
const right = extractMongooseDef(unwrapped._def.right, visited, isField, true);
|
|
783
|
-
if (typeof left === "object" && typeof right === "object") {
|
|
784
|
-
Object.assign(mongooseProp, left, right);
|
|
785
|
-
if (isField && !noWrap && mongooseProp.schema !== false && !mongooseProp.type) {
|
|
786
|
-
const mongoose = getMongoose();
|
|
787
|
-
if (mongoose) {
|
|
788
|
-
const options = typeof mongooseProp.schema === "object" ? mongooseProp.schema : {};
|
|
789
|
-
const { plugins, ...schemaOptions } = options;
|
|
790
|
-
const definition = { ...mongooseProp };
|
|
791
|
-
delete definition.schema;
|
|
792
|
-
const subSchema = new mongoose.Schema(definition, schemaOptions);
|
|
793
|
-
if (plugins && Array.isArray(plugins)) {
|
|
794
|
-
for (const plugin of plugins) {
|
|
795
|
-
subSchema.plugin(plugin);
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
mongooseProp.type = subSchema;
|
|
799
|
-
for (const key of Object.keys(mongooseProp)) {
|
|
800
|
-
if (key !== "type")
|
|
801
|
-
delete mongooseProp[key];
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
}
|
|
805
|
-
} else if (!mongooseProp.type) {
|
|
806
|
-
mongooseProp.type = ((_a = getMongoose()) == null ? void 0 : _a.Schema.Types.Mixed) || "Mixed";
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
if ((type === "union" || type === "discriminatedunion" || type === "discriminated_union" || type === "xor") && !mongooseProp.type) {
|
|
810
|
-
const mongoose = getMongoose();
|
|
811
|
-
const options = unwrapped.options || unwrapped._def.options;
|
|
812
|
-
const discriminatorKey = unwrapped._def.discriminator;
|
|
813
|
-
const unionCtx = {
|
|
814
|
-
isSimpleUnion: false,
|
|
815
|
-
isObjectUnion: false,
|
|
816
|
-
isXor: type === "xor" || (((_b = unwrapped._def) == null ? void 0 : _b.inclusive) === false || ((_c = schema._def) == null ? void 0 : _c.inclusive) === false) && !discriminatorKey && !((_d = schema._def) == null ? void 0 : _d.discriminator)
|
|
817
|
-
};
|
|
818
|
-
if (Array.isArray(options) && options.length > 0) {
|
|
819
|
-
unionCtx.isSimpleUnion = options.every((opt) => {
|
|
820
|
-
const { type: type2 } = unwrapZodSchema(opt).schema._def;
|
|
821
|
-
return ["string", "number", "boolean", "date", "bigint", "literal"].includes(type2);
|
|
822
|
-
});
|
|
823
|
-
unionCtx.isObjectUnion = options.every((opt) => {
|
|
824
|
-
const { type: type2 } = unwrapZodSchema(opt).schema._def;
|
|
825
|
-
return type2 === "object";
|
|
826
|
-
});
|
|
827
|
-
}
|
|
828
|
-
callHookSync("schema:union:before", { schema: unwrapped, mongooseProp, ctx: unionCtx });
|
|
829
|
-
if (discriminatorKey && unionCtx.isObjectUnion) {
|
|
830
|
-
const discriminators = {};
|
|
831
|
-
const allOptionDefs = [];
|
|
832
|
-
for (const option of options) {
|
|
833
|
-
const { schema: unwrappedOpt } = unwrapZodSchema(option);
|
|
834
|
-
const { shape } = unwrappedOpt._def;
|
|
835
|
-
const discriminatorProp = shape[discriminatorKey];
|
|
836
|
-
const { schema: unwrappedDisc } = unwrapZodSchema(discriminatorProp);
|
|
837
|
-
const discriminatorValue = (_f = unwrappedDisc._def.value) != null ? _f : (_e = unwrappedDisc._def.values) == null ? void 0 : _e[0];
|
|
838
|
-
const optionDef = extractMongooseDef(option, /* @__PURE__ */ new Map(), true, true);
|
|
839
|
-
if (optionDef && typeof optionDef === "object" && !Array.isArray(optionDef)) {
|
|
840
|
-
const cleanOptionDef = { ...optionDef };
|
|
841
|
-
delete cleanOptionDef[discriminatorKey];
|
|
842
|
-
discriminators[discriminatorValue] = cleanOptionDef;
|
|
843
|
-
allOptionDefs.push(cleanOptionDef);
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
const baseDef = {};
|
|
847
|
-
if (allOptionDefs.length > 0) {
|
|
848
|
-
const firstOption = allOptionDefs[0];
|
|
849
|
-
for (const key of Object.keys(firstOption)) {
|
|
850
|
-
const isCommon = allOptionDefs.every((def2) => {
|
|
851
|
-
if (!(key in def2))
|
|
852
|
-
return false;
|
|
853
|
-
return areDefinitionsEqual(def2[key], firstOption[key]);
|
|
854
|
-
});
|
|
855
|
-
if (isCommon) {
|
|
856
|
-
baseDef[key] = firstOption[key];
|
|
857
|
-
for (const def2 of allOptionDefs) {
|
|
858
|
-
delete def2[key];
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
const result = {
|
|
864
|
-
__isDiscriminatorUnion: true,
|
|
865
|
-
discriminatorKey,
|
|
866
|
-
discriminators,
|
|
867
|
-
baseDef,
|
|
868
|
-
validate: {
|
|
869
|
-
validator(_v) {
|
|
870
|
-
var _a2, _b2;
|
|
871
|
-
try {
|
|
872
|
-
const mongoose2 = getMongoose();
|
|
873
|
-
const doc = mongoose2 && this instanceof mongoose2.Document ? this.toObject() : this || {};
|
|
874
|
-
schema.parse(doc);
|
|
875
|
-
return true;
|
|
876
|
-
} catch (err) {
|
|
877
|
-
const message = ((_b2 = (_a2 = err == null ? void 0 : err.errors) == null ? void 0 : _a2[0]) == null ? void 0 : _b2.message) || err.message;
|
|
878
|
-
if (this && typeof this.invalidate === "function") {
|
|
879
|
-
this.invalidate(discriminatorKey, `Zod validation failed: ${message}`);
|
|
880
|
-
}
|
|
881
|
-
return false;
|
|
882
|
-
}
|
|
883
|
-
},
|
|
884
|
-
message: (props) => `Validation failed for ${props.path}`
|
|
885
|
-
}
|
|
886
|
-
};
|
|
887
|
-
if (mongooseProp && typeof mongooseProp === "object" && !Array.isArray(mongooseProp)) {
|
|
888
|
-
Object.assign(mongooseProp, result);
|
|
889
|
-
callHookSync("schema:union:after", {
|
|
890
|
-
schema: unwrapped,
|
|
891
|
-
mongooseProp,
|
|
892
|
-
ctx: unionCtx
|
|
893
|
-
});
|
|
894
|
-
return mongooseProp;
|
|
895
|
-
}
|
|
896
|
-
callHookSync("schema:union:after", {
|
|
897
|
-
schema: unwrapped,
|
|
898
|
-
mongooseProp: result,
|
|
899
|
-
ctx: unionCtx
|
|
900
|
-
});
|
|
901
|
-
return result;
|
|
902
|
-
}
|
|
903
|
-
if (((_g = getMongoose()) == null ? void 0 : _g.Schema.Types.Union) && unionCtx.isSimpleUnion && options.length > 0 && !unionCtx.isXor) {
|
|
904
|
-
mongooseProp.type = mongoose.Schema.Types.Union;
|
|
905
|
-
mongooseProp.of = options.map((opt) => {
|
|
906
|
-
const def2 = extractMongooseDef(opt, visited, true, true);
|
|
907
|
-
return def2.type || def2;
|
|
908
|
-
});
|
|
909
|
-
} else if (unionCtx.isObjectUnion && options.length > 0) {
|
|
910
|
-
const mergedDef = {};
|
|
911
|
-
for (const opt of options) {
|
|
912
|
-
const def2 = extractMongooseDef(opt, /* @__PURE__ */ new Map(), true, true);
|
|
913
|
-
if (typeof def2 === "object" && def2 !== null) {
|
|
914
|
-
for (const [key, prop] of Object.entries(def2)) {
|
|
915
|
-
if (typeof prop === "object" && prop !== null && !Array.isArray(prop)) {
|
|
916
|
-
prop.required = false;
|
|
917
|
-
}
|
|
918
|
-
if (mergedDef[key] && typeof mergedDef[key] === "object" && typeof prop === "object" && !Array.isArray(mergedDef[key]) && !Array.isArray(prop)) {
|
|
919
|
-
const existingType = mergedDef[key].type || mergedDef[key].instance || (typeof mergedDef[key] === "function" ? mergedDef[key] : null);
|
|
920
|
-
const newType = prop.type || prop.instance || (typeof prop === "function" ? prop : null);
|
|
921
|
-
const isMixed = (t) => {
|
|
922
|
-
var _a2, _b2;
|
|
923
|
-
return !t || t === "Mixed" || t === "SchemaMixed" || (t == null ? void 0 : t.name) === "Mixed" || (t == null ? void 0 : t.instance) === "Mixed" || (t == null ? void 0 : t.name) === "SchemaMixed" || (t == null ? void 0 : t.instance) === "SchemaMixed" || ((_a2 = getMongoose()) == null ? void 0 : _a2.Schema.Types.Mixed) && (t === ((_b2 = getMongoose()) == null ? void 0 : _b2.Schema.Types.Mixed) || (t == null ? void 0 : t.instance) === "Mixed" || (t == null ? void 0 : t.instance) === "SchemaMixed");
|
|
924
|
-
};
|
|
925
|
-
if (isMixed(existingType) && !isMixed(newType)) {
|
|
926
|
-
mergedDef[key] = prop;
|
|
927
|
-
} else if (!isMixed(existingType) && isMixed(newType)) ;
|
|
928
|
-
else {
|
|
929
|
-
Object.assign(mergedDef[key], prop);
|
|
930
|
-
}
|
|
931
|
-
} else if (!mergedDef[key] || typeof mergedDef[key] !== "object" || Array.isArray(mergedDef[key])) {
|
|
932
|
-
mergedDef[key] = prop;
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
if (isField && unionCtx.isXor) {
|
|
938
|
-
mongooseProp.type = (mongoose == null ? void 0 : mongoose.Schema.Types.Mixed) || "Mixed";
|
|
939
|
-
mongooseProp.validate = {
|
|
940
|
-
validator(v) {
|
|
941
|
-
try {
|
|
942
|
-
schema.parse(v);
|
|
943
|
-
return true;
|
|
944
|
-
} catch {
|
|
945
|
-
return false;
|
|
946
|
-
}
|
|
947
|
-
},
|
|
948
|
-
message: "XOR validation failed"
|
|
949
|
-
};
|
|
950
|
-
} else {
|
|
951
|
-
if (!mongooseProp.type || mongooseProp.type === (((_h = getMongoose()) == null ? void 0 : _h.Schema.Types.Mixed) || "Mixed")) {
|
|
952
|
-
delete mongooseProp.type;
|
|
953
|
-
}
|
|
954
|
-
Object.assign(mongooseProp, mergedDef);
|
|
955
|
-
if (isField && Object.prototype.hasOwnProperty.call(mongooseProp, "type") && Object.keys(mongooseProp).length > 1) {
|
|
956
|
-
const mongooseInstance3 = getMongoose();
|
|
957
|
-
if (mongooseInstance3) {
|
|
958
|
-
mongooseProp.type = new mongooseInstance3.Schema(mongooseProp, { _id: false });
|
|
959
|
-
for (const key of Object.keys(mongooseProp)) {
|
|
960
|
-
if (key !== "type")
|
|
961
|
-
delete mongooseProp[key];
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
} else {
|
|
967
|
-
mongooseProp.type = (mongoose == null ? void 0 : mongoose.Schema.Types.Mixed) || "Mixed";
|
|
968
|
-
if (isField && (type === "xor" || type === "discriminated_union" || type === "discriminatedunion" || type === "union") && !mongooseProp.ref && // Skip Zod validation for populated fields
|
|
969
|
-
!Array.isArray(mongooseProp.type) && // Skip Zod validation for arrays
|
|
970
|
-
mongooseProp.type !== Map && // Skip Zod validation for maps
|
|
971
|
-
!mongooseProp.of) {
|
|
972
|
-
mongooseProp.validate = {
|
|
973
|
-
validator(v) {
|
|
974
|
-
try {
|
|
975
|
-
schema.parse(v);
|
|
976
|
-
return true;
|
|
977
|
-
} catch (err) {
|
|
978
|
-
return false;
|
|
979
|
-
}
|
|
980
|
-
},
|
|
981
|
-
message: (props) => {
|
|
982
|
-
if (unionCtx.isXor)
|
|
983
|
-
return "XOR validation failed";
|
|
984
|
-
try {
|
|
985
|
-
schema.parse(props.value);
|
|
986
|
-
} catch (err) {
|
|
987
|
-
return `Union validation failed: ${err.message}`;
|
|
988
|
-
}
|
|
989
|
-
return "Union validation failed";
|
|
990
|
-
}
|
|
991
|
-
};
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
callHookSync("schema:union:after", {
|
|
995
|
-
schema: unwrapped,
|
|
996
|
-
mongooseProp,
|
|
997
|
-
ctx: unionCtx
|
|
998
|
-
});
|
|
999
|
-
}
|
|
1000
|
-
if (type === "literal" && !mongooseProp.type) {
|
|
1001
|
-
mongooseProp.type = ((_i = getMongoose()) == null ? void 0 : _i.Schema.Types.Mixed) || "Mixed";
|
|
1002
|
-
}
|
|
1003
|
-
switch (type) {
|
|
1004
|
-
case "string":
|
|
1005
|
-
case "number":
|
|
1006
|
-
case "boolean":
|
|
1007
|
-
case "date":
|
|
1008
|
-
case "bigint":
|
|
1009
|
-
case "stringbool":
|
|
1010
|
-
case "boolstring":
|
|
1011
|
-
case "booleanstring": {
|
|
1012
|
-
if (!mongooseProp.type) {
|
|
1013
|
-
if (type === "bigint") {
|
|
1014
|
-
mongooseProp.type = typeof BigInt === "undefined" ? Number : BigInt;
|
|
1015
|
-
} else {
|
|
1016
|
-
const typeMap = {
|
|
1017
|
-
string: String,
|
|
1018
|
-
number: Number,
|
|
1019
|
-
boolean: Boolean,
|
|
1020
|
-
date: Date,
|
|
1021
|
-
stringbool: Boolean,
|
|
1022
|
-
boolstring: Boolean,
|
|
1023
|
-
booleanstring: Boolean
|
|
1024
|
-
};
|
|
1025
|
-
mongooseProp.type = typeMap[type];
|
|
1026
|
-
if (mongooseProp.default !== void 0) {
|
|
1027
|
-
const defaultType = typeof mongooseProp.default;
|
|
1028
|
-
if (defaultType === "boolean" && type !== "boolean") {
|
|
1029
|
-
mongooseProp.type = Boolean;
|
|
1030
|
-
} else if (defaultType === "number" && type !== "number") {
|
|
1031
|
-
mongooseProp.type = Number;
|
|
1032
|
-
} else if (defaultType === "string" && type !== "string") {
|
|
1033
|
-
mongooseProp.type = String;
|
|
1034
|
-
} else if (mongooseProp.default instanceof Date && type !== "date") {
|
|
1035
|
-
mongooseProp.type = Date;
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
if (mongooseProp.type === String && features.transformations) {
|
|
1039
|
-
for (const tx of features.transformations) {
|
|
1040
|
-
const txStr = ((_j = tx.transform) == null ? void 0 : _j.toString()) || tx.toString();
|
|
1041
|
-
if (txStr.includes("stringbool") || txStr.includes("boolstring") || txStr.includes("booleanstring")) {
|
|
1042
|
-
mongooseProp.type = Boolean;
|
|
1043
|
-
break;
|
|
1044
|
-
}
|
|
1045
|
-
if (txStr.includes('=== "true"') || txStr.includes("=== 'true'")) {
|
|
1046
|
-
mongooseProp.type = Boolean;
|
|
1047
|
-
break;
|
|
1048
|
-
}
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
|
-
}
|
|
1053
|
-
if (mongooseProp.required !== false)
|
|
1054
|
-
mongooseProp.required = true;
|
|
1055
|
-
break;
|
|
1056
|
-
}
|
|
1057
|
-
case "enum":
|
|
1058
|
-
case "nativeenum":
|
|
1059
|
-
case "native_enum": {
|
|
1060
|
-
if (!mongooseProp.type)
|
|
1061
|
-
mongooseProp.type = String;
|
|
1062
|
-
mongooseProp.enum = type === "enum" ? unwrapped.options || def.values : Object.values(unwrapped.enum || def.values);
|
|
1063
|
-
if (mongooseProp.required !== false)
|
|
1064
|
-
mongooseProp.required = true;
|
|
1065
|
-
break;
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
const mongooseInstance2 = getMongoose();
|
|
1069
|
-
if (type === "any" || type === "unknown" || type === "custom") {
|
|
1070
|
-
const cls = def.cls || unwrapped.cls;
|
|
1071
|
-
if (cls === Buffer || typeof Uint8Array !== "undefined" && cls === Uint8Array) {
|
|
1072
|
-
if (!mongooseProp.type)
|
|
1073
|
-
mongooseProp.type = (mongooseInstance2 == null ? void 0 : mongooseInstance2.Schema.Types.Buffer) || "Buffer";
|
|
1074
|
-
} else if (((cls == null ? void 0 : cls.name) === "ObjectId" || mongooseInstance2 && cls === mongooseInstance2.Types.ObjectId) && !mongooseProp.type) {
|
|
1075
|
-
mongooseProp.type = (mongooseInstance2 == null ? void 0 : mongooseInstance2.Schema.Types.ObjectId) || "ObjectId";
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
if (type === "lazy") {
|
|
1079
|
-
const inner = def.getter();
|
|
1080
|
-
const result = extractMongooseDef(inner, visited, isField);
|
|
1081
|
-
if (Object.keys(meta).length > 0 && result !== mongooseProp) {
|
|
1082
|
-
if (typeof result === "object" && !Array.isArray(result)) {
|
|
1083
|
-
Object.assign(mongooseProp, result);
|
|
1084
|
-
} else {
|
|
1085
|
-
mongooseProp.type = result.type || result;
|
|
1086
|
-
}
|
|
1087
|
-
return mongooseProp;
|
|
1088
|
-
}
|
|
1089
|
-
return result;
|
|
1090
|
-
}
|
|
1091
|
-
if (!mongooseProp.type && type !== "object") {
|
|
1092
|
-
mongooseProp.type = ((_k = getMongoose()) == null ? void 0 : _k.Schema.Types.Mixed) || "Mixed";
|
|
1093
|
-
}
|
|
1094
|
-
callHookSync("converter:after", {
|
|
1095
|
-
schema,
|
|
1096
|
-
mongooseProp
|
|
1097
|
-
});
|
|
1098
|
-
if (typeof mongooseProp === "object" && mongooseProp !== null && !Array.isArray(mongooseProp)) {
|
|
1099
|
-
delete mongooseProp.includeId;
|
|
1100
|
-
}
|
|
1101
|
-
return mongooseProp;
|
|
1102
|
-
}
|
|
1103
|
-
function modelNameFromCollection(collection) {
|
|
1104
|
-
const singular = collection.replace(/ies$/i, "y").replace(/(ses|xes|zes|ches|shes)$/i, (suffix) => suffix.slice(0, -2)).replace(/s$/i, "");
|
|
1105
|
-
return singular.slice(0, 1).toUpperCase() + singular.slice(1);
|
|
1106
|
-
}
|
|
1107
|
-
function toMongooseSchema(schema, options) {
|
|
1108
|
-
var _a, _b;
|
|
1109
|
-
const { schema: unwrapped } = unwrapZodSchema(schema);
|
|
1110
|
-
const meta = mongooseRegistry.get(schema) || mongooseRegistry.get(unwrapped) || ((_a = schema.meta) == null ? void 0 : _a.call(schema)) || ((_b = unwrapped.meta) == null ? void 0 : _b.call(unwrapped)) || {};
|
|
1111
|
-
const { plugins, modelName, ...schemaOptions } = options || {};
|
|
1112
|
-
const mergedOptions = {
|
|
1113
|
-
...unwrapped instanceof z.ZodObject ? { strict: objectStrictness(unwrapped) } : {},
|
|
1114
|
-
...unwrapped instanceof z.ZodObject && objectStrictness(unwrapped) === false ? { minimize: false } : {},
|
|
1115
|
-
// Also merge other schema options from meta if they exist
|
|
1116
|
-
...meta.collection ? { collection: meta.collection } : {},
|
|
1117
|
-
// eslint-disable-next-line unicorn/no-negated-condition
|
|
1118
|
-
...meta.strict !== void 0 ? { strict: meta.strict } : {},
|
|
1119
|
-
// eslint-disable-next-line unicorn/no-negated-condition
|
|
1120
|
-
...meta.minimize !== void 0 ? { minimize: meta.minimize } : {},
|
|
1121
|
-
// eslint-disable-next-line unicorn/no-negated-condition
|
|
1122
|
-
...meta.validateBeforeSave !== void 0 ? { validateBeforeSave: meta.validateBeforeSave } : {},
|
|
1123
|
-
// eslint-disable-next-line unicorn/no-negated-condition
|
|
1124
|
-
...meta.versionKey !== void 0 ? { versionKey: meta.versionKey } : {},
|
|
1125
|
-
...meta.id === void 0 ? {} : { id: meta.id },
|
|
1126
|
-
...meta._id === void 0 ? {} : { _id: meta._id },
|
|
1127
|
-
...meta.timestamps ? { timestamps: meta.timestamps } : {},
|
|
1128
|
-
...meta.discriminatorKey ? { discriminatorKey: meta.discriminatorKey } : {},
|
|
1129
|
-
...schemaOptions
|
|
1130
|
-
};
|
|
1131
|
-
let definition = extractMongooseDef(schema, /* @__PURE__ */ new Map(), false);
|
|
1132
|
-
const mongoose = getMongoose();
|
|
1133
|
-
if (!mongoose) {
|
|
1134
|
-
throw new Error("Mongoose must be installed to use toMongooseSchema. If you are in an ESM environment, ensure mongoose is loaded.");
|
|
1135
|
-
}
|
|
1136
|
-
let mongooseSchema;
|
|
1137
|
-
if (definition && typeof definition === "object" && definition.__isDiscriminatorUnion) {
|
|
1138
|
-
const baseModelName = modelName || schemaOptions.collection && modelNameFromCollection(schemaOptions.collection);
|
|
1139
|
-
if (!baseModelName) {
|
|
1140
|
-
throw new Error("toMongooseSchema() found a top-level discriminated union. Provide `modelName` or `collection` so discriminator model names can be made unique, for example `{ modelName: 'Activity' }`.");
|
|
1141
|
-
}
|
|
1142
|
-
const { discriminatorKey, discriminators, baseDef, validate } = definition;
|
|
1143
|
-
mongooseSchema = new mongoose.Schema(baseDef, {
|
|
1144
|
-
...mergedOptions,
|
|
1145
|
-
discriminatorKey
|
|
1146
|
-
});
|
|
1147
|
-
if (validate) {
|
|
1148
|
-
if (!mongooseSchema.path(discriminatorKey)) {
|
|
1149
|
-
mongooseSchema.add({ [discriminatorKey]: { type: String } });
|
|
1150
|
-
}
|
|
1151
|
-
mongooseSchema.path(discriminatorKey).validate(validate);
|
|
1152
|
-
}
|
|
1153
|
-
for (const [key, dDef] of Object.entries(discriminators)) {
|
|
1154
|
-
const discriminatorModelName = `${baseModelName}_${key}`;
|
|
1155
|
-
if (mongooseSchema.discriminators && mongooseSchema.discriminators[discriminatorModelName]) {
|
|
1156
|
-
continue;
|
|
1157
|
-
}
|
|
1158
|
-
mongooseSchema.discriminator(discriminatorModelName, new mongoose.Schema(dDef, { _id: false }), { value: key });
|
|
1159
|
-
}
|
|
1160
|
-
} else {
|
|
1161
|
-
if (typeof definition === "object" && definition !== null) {
|
|
1162
|
-
const { includeId, ...cleanDefinition } = definition;
|
|
1163
|
-
definition = cleanDefinition;
|
|
1164
|
-
for (const value of Object.values(definition)) {
|
|
1165
|
-
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
1166
|
-
delete value.includeId;
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
mongooseSchema = new mongoose.Schema(definition, mergedOptions);
|
|
1171
|
-
}
|
|
1172
|
-
if (mergedOptions.validateBeforeSave !== false) {
|
|
1173
|
-
mongooseSchema.post("validate", function() {
|
|
1174
|
-
var _a2, _b2, _c, _d;
|
|
1175
|
-
try {
|
|
1176
|
-
schema.parse(prepareForZodValidation(schema, this.toObject(), mongooseSchema));
|
|
1177
|
-
} catch (e) {
|
|
1178
|
-
if (e instanceof z.ZodError) {
|
|
1179
|
-
e.message = JSON.stringify({
|
|
1180
|
-
context: {
|
|
1181
|
-
model: ((_a2 = this.constructor) == null ? void 0 : _a2.modelName) || ((_b2 = this.constructor) == null ? void 0 : _b2.name) || "_unknown_",
|
|
1182
|
-
id: ((_c = this._id) == null ? void 0 : _c.toString()) || ((_d = this.id) == null ? void 0 : _d.toString()) || "_unknown_"
|
|
1183
|
-
},
|
|
1184
|
-
errors: e.issues || e.errors || []
|
|
1185
|
-
});
|
|
1186
|
-
}
|
|
1187
|
-
throw e;
|
|
1188
|
-
}
|
|
1189
|
-
});
|
|
1190
|
-
}
|
|
1191
|
-
if (plugins && Array.isArray(plugins)) {
|
|
1192
|
-
for (const plugin of plugins) {
|
|
1193
|
-
mongooseSchema.plugin(plugin);
|
|
1194
|
-
}
|
|
1195
|
-
}
|
|
1196
|
-
callHookSync("schema:created", {
|
|
1197
|
-
schema,
|
|
1198
|
-
mongooseSchema,
|
|
1199
|
-
options: mergedOptions
|
|
1200
|
-
});
|
|
1201
|
-
return mongooseSchema;
|
|
1202
|
-
}
|
|
1203
|
-
function populateZodSchema(schema, keys) {
|
|
1204
|
-
const { shape } = schema;
|
|
1205
|
-
const newShape = { ...shape };
|
|
1206
|
-
const keysToPopulate = keys || Object.keys(shape);
|
|
1207
|
-
const populateField = (field) => {
|
|
1208
|
-
const meta = getMongooseMeta(field);
|
|
1209
|
-
const { schema: unwrapped, features } = unwrapZodSchema(field);
|
|
1210
|
-
let result = field;
|
|
1211
|
-
if (meta == null ? void 0 : meta.refSchema) {
|
|
1212
|
-
result = meta.refSchema;
|
|
1213
|
-
} else if (unwrapped instanceof z.ZodArray) {
|
|
1214
|
-
const populatedInner = populateField(unwrapped.element);
|
|
1215
|
-
if (populatedInner !== unwrapped.element) {
|
|
1216
|
-
result = z.array(populatedInner);
|
|
1217
|
-
}
|
|
1218
|
-
} else if (unwrapped instanceof z.ZodObject) {
|
|
1219
|
-
result = populateZodSchema(unwrapped);
|
|
1220
|
-
}
|
|
1221
|
-
if (result !== field && result !== unwrapped) {
|
|
1222
|
-
if (features.isOptional && !(result instanceof z.ZodOptional))
|
|
1223
|
-
result = result.optional();
|
|
1224
|
-
if (features.isNullable && !(result instanceof z.ZodNullable))
|
|
1225
|
-
result = result.nullable();
|
|
1226
|
-
if (features.default !== void 0 && !(result instanceof z.ZodDefault))
|
|
1227
|
-
result = result.default(features.default);
|
|
1228
|
-
}
|
|
1229
|
-
return result;
|
|
1230
|
-
};
|
|
1231
|
-
for (const key of keysToPopulate) {
|
|
1232
|
-
newShape[key] = populateField(shape[key]);
|
|
1233
|
-
}
|
|
1234
|
-
return z.object(newShape);
|
|
1235
|
-
}
|
|
1236
|
-
const genTimestampsSchema = (createdAtField = "createdAt", updatedAtField = "updatedAt") => {
|
|
1237
|
-
if (createdAtField != null && updatedAtField != null && createdAtField === updatedAtField) {
|
|
1238
|
-
throw new Error("`createdAt` and `updatedAt` fields must be different");
|
|
1239
|
-
}
|
|
1240
|
-
const shape = {};
|
|
1241
|
-
if (createdAtField != null)
|
|
1242
|
-
shape[createdAtField] = z.date().default(() => /* @__PURE__ */ new Date());
|
|
1243
|
-
if (updatedAtField != null)
|
|
1244
|
-
shape[updatedAtField] = z.date().default(() => /* @__PURE__ */ new Date());
|
|
1245
|
-
return shape;
|
|
1246
|
-
};
|
|
1247
|
-
const bufferMongooseGetter = (value) => value != null && value._bsontype === "Binary" ? value.buffer : value;
|
|
1248
|
-
const position = z.tuple([z.number(), z.number()]);
|
|
1249
|
-
const zPoint = (options) => withMongoose(z.object({
|
|
1250
|
-
type: withMongoose(z.literal("Point"), { type: String, enum: ["Point"], required: true }),
|
|
1251
|
-
coordinates: withMongoose(position, { required: true })
|
|
1252
|
-
}), { schema: { _id: false }, required: true, ...options });
|
|
1253
|
-
const zPolygon = (options) => withMongoose(z.object({
|
|
1254
|
-
type: withMongoose(z.literal("Polygon"), { type: String, enum: ["Polygon"], required: true }),
|
|
1255
|
-
coordinates: withMongoose(z.array(z.array(position).refine((ring) => {
|
|
1256
|
-
var _a, _b, _c, _d;
|
|
1257
|
-
return ring.length >= 4 && ((_a = ring[0]) == null ? void 0 : _a[0]) === ((_b = ring.at(-1)) == null ? void 0 : _b[0]) && ((_c = ring[0]) == null ? void 0 : _c[1]) === ((_d = ring.at(-1)) == null ? void 0 : _d[1]);
|
|
1258
|
-
}, "Polygon rings must be closed")).refine((rings) => rings.length > 0, "Polygon must have at least one ring"), { required: true })
|
|
1259
|
-
}), { schema: { _id: false }, required: true, ...options });
|
|
1260
|
-
const preprocessFn = (val) => val === null ? void 0 : val;
|
|
1261
|
-
const zObjectId = (options) => {
|
|
1262
|
-
var _a;
|
|
1263
|
-
const objectIdSchema = z.custom((val) => {
|
|
1264
|
-
const mongooseInstance2 = getMongoose();
|
|
1265
|
-
return mongooseInstance2 && val instanceof mongooseInstance2.Types.ObjectId;
|
|
1266
|
-
});
|
|
1267
|
-
const baseUnion = z.preprocess(preprocessFn, z.union([objectIdSchema, z.string().regex(/^[\dA-Fa-f]{24}$/, "Invalid ObjectId")]));
|
|
1268
|
-
const inputSchema = z.codec(baseUnion, objectIdSchema, {
|
|
1269
|
-
decode: (val) => {
|
|
1270
|
-
const mongooseInstance2 = getMongoose();
|
|
1271
|
-
if (!mongooseInstance2)
|
|
1272
|
-
return val;
|
|
1273
|
-
if (val instanceof mongooseInstance2.Types.ObjectId) {
|
|
1274
|
-
return val;
|
|
1275
|
-
}
|
|
1276
|
-
return new mongooseInstance2.Types.ObjectId(val);
|
|
1277
|
-
},
|
|
1278
|
-
encode: (val) => val.toString()
|
|
1279
|
-
});
|
|
1280
|
-
return withMongoose(inputSchema, {
|
|
1281
|
-
type: ((_a = getMongoose()) == null ? void 0 : _a.Schema.Types.ObjectId) || "ObjectId",
|
|
1282
|
-
...options
|
|
1283
|
-
});
|
|
1284
|
-
};
|
|
1285
|
-
const zBuffer = (options) => {
|
|
1286
|
-
var _a;
|
|
1287
|
-
return withMongoose(z.preprocess((val) => {
|
|
1288
|
-
var _a2;
|
|
1289
|
-
const Binary = (_a2 = getMongoose()) == null ? void 0 : _a2.mongo.Binary;
|
|
1290
|
-
return Binary && val instanceof Binary ? Buffer.from(val.value()) : val;
|
|
1291
|
-
}, z.custom((val) => val instanceof Uint8Array)), { type: ((_a = getMongoose()) == null ? void 0 : _a.Schema.Types.Buffer) || "Buffer", ...options });
|
|
1292
|
-
};
|
|
1293
|
-
const zRef = (ref, schema, options) => {
|
|
1294
|
-
var _a;
|
|
1295
|
-
const objectIdSchema = zObjectId();
|
|
1296
|
-
const base = z.codec(z.union([objectIdSchema, schema]), objectIdSchema, {
|
|
1297
|
-
decode: (val) => typeof val === "object" && val !== null && "_id" in val ? val._id : val,
|
|
1298
|
-
encode: (val) => val
|
|
1299
|
-
});
|
|
1300
|
-
return withMongoose(base, {
|
|
1301
|
-
type: ((_a = getMongoose()) == null ? void 0 : _a.Schema.Types.ObjectId) || "ObjectId",
|
|
1302
|
-
ref,
|
|
1303
|
-
refSchema: schema,
|
|
1304
|
-
...options
|
|
1305
|
-
});
|
|
1306
|
-
};
|
|
1307
|
-
function toStrictModel(name, mongooseSchema) {
|
|
1308
|
-
const m = getMongoose();
|
|
1309
|
-
if (!m) {
|
|
1310
|
-
throw new Error("Mongoose must be installed to use toStrictModel.");
|
|
1311
|
-
}
|
|
1312
|
-
const rawModel = m.model(name, mongooseSchema);
|
|
1313
|
-
return rawModel;
|
|
1314
|
-
}
|
|
1315
|
-
|
|
1316
|
-
const mongooseZod = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
|
|
1317
|
-
__proto__: null,
|
|
1318
|
-
bufferMongooseGetter: bufferMongooseGetter,
|
|
1319
|
-
callHookSync: callHookSync,
|
|
1320
|
-
extractMongooseDef: extractMongooseDef,
|
|
1321
|
-
genTimestampsSchema: genTimestampsSchema,
|
|
1322
|
-
getFrontendMode: getFrontendMode,
|
|
1323
|
-
getMongoose: getMongoose,
|
|
1324
|
-
getMongooseMeta: getMongooseMeta,
|
|
1325
|
-
hooks: hooks,
|
|
1326
|
-
mongooseRegistry: mongooseRegistry,
|
|
1327
|
-
objectStrictness: objectStrictness,
|
|
1328
|
-
populateZodSchema: populateZodSchema,
|
|
1329
|
-
setFrontendMode: setFrontendMode,
|
|
1330
|
-
setMongoose: setMongoose,
|
|
1331
|
-
toMongooseSchema: toMongooseSchema,
|
|
1332
|
-
toStrictModel: toStrictModel,
|
|
1333
|
-
unwrapZodSchema: unwrapZodSchema,
|
|
1334
|
-
withMongoose: withMongoose,
|
|
1335
|
-
zBuffer: zBuffer,
|
|
1336
|
-
zObjectId: zObjectId,
|
|
1337
|
-
zPoint: zPoint,
|
|
1338
|
-
zPolygon: zPolygon,
|
|
1339
|
-
zRef: zRef
|
|
1340
|
-
}, Symbol.toStringTag, { value: 'Module' }));
|
|
1341
|
-
|
|
1342
|
-
setMongoose(mongoose.default || mongoose);
|
|
1343
|
-
let _filename = "/sandbox/main.ts";
|
|
1344
|
-
let _dirname = "/sandbox";
|
|
1345
|
-
try {
|
|
1346
|
-
_filename = fileURLToPath(globalThis._importMeta_.url);
|
|
1347
|
-
_dirname = dirname(_filename);
|
|
1348
|
-
} catch {
|
|
1349
|
-
}
|
|
19
|
+
const MAX_PENDING_PARSES = 3;
|
|
1350
20
|
let parseQueue = Promise.resolve();
|
|
1351
|
-
|
|
21
|
+
let pendingParses = 0;
|
|
22
|
+
async function acquireLocalLock() {
|
|
23
|
+
if (pendingParses >= MAX_PENDING_PARSES) throw createError({ statusCode: 503, message: "Studio is busy. Try again shortly." });
|
|
24
|
+
pendingParses++;
|
|
1352
25
|
const previous = parseQueue;
|
|
1353
26
|
let release;
|
|
1354
27
|
parseQueue = new Promise((resolve) => {
|
|
1355
|
-
release =
|
|
28
|
+
release = () => {
|
|
29
|
+
pendingParses--;
|
|
30
|
+
resolve();
|
|
31
|
+
};
|
|
1356
32
|
});
|
|
1357
33
|
await previous;
|
|
1358
34
|
return release;
|
|
1359
35
|
}
|
|
1360
|
-
function getErrorMessage(error) {
|
|
1361
|
-
if (error instanceof Error) return error.message || error.name;
|
|
1362
|
-
return String(error);
|
|
1363
|
-
}
|
|
1364
|
-
if (typeof __filename === "undefined") {
|
|
1365
|
-
globalThis.__filename = _filename;
|
|
1366
|
-
}
|
|
1367
|
-
if (typeof __dirname === "undefined") {
|
|
1368
|
-
globalThis.__dirname = _dirname;
|
|
1369
|
-
}
|
|
1370
36
|
const parse_post = defineEventHandler(async (event) => {
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
const { sourceCode } = await readBody(event);
|
|
1374
|
-
if (!sourceCode) {
|
|
1375
|
-
throw createError({ statusCode: 400, message: "Missing sourceCode" });
|
|
37
|
+
if (useRuntimeConfig(event).public.isDocsMode) {
|
|
38
|
+
throw createError({ statusCode: 403, message: "Local Studio execution is unavailable in docs mode." });
|
|
1376
39
|
}
|
|
1377
|
-
const
|
|
40
|
+
const body = await readStudioRequest(event);
|
|
41
|
+
const release = await acquireLocalLock();
|
|
1378
42
|
try {
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
jsCode = transformSync(sourceCode, {
|
|
1383
|
-
loader: "ts",
|
|
1384
|
-
format: "cjs",
|
|
1385
|
-
target: "esnext",
|
|
1386
|
-
// Provide these globals during transpilation just in case
|
|
1387
|
-
define: {
|
|
1388
|
-
__filename: '"/sandbox/main.ts"',
|
|
1389
|
-
__dirname: '"/sandbox"'
|
|
1390
|
-
}
|
|
1391
|
-
}).code;
|
|
1392
|
-
} catch (error) {
|
|
1393
|
-
throw createError({
|
|
1394
|
-
statusCode: 400,
|
|
1395
|
-
message: `Compilation Error: ${getErrorMessage(error)}`
|
|
1396
|
-
});
|
|
1397
|
-
}
|
|
1398
|
-
const sandboxRequire = (moduleName) => {
|
|
1399
|
-
if (moduleName === "zod" || moduleName === "zod/v4") return zod;
|
|
1400
|
-
if (moduleName === "@nullix/zod-mongoose") return mongooseZod;
|
|
1401
|
-
if (moduleName === "mongoose") return mongoose.default || mongoose;
|
|
1402
|
-
throw new Error(`Security Exception: Module "${moduleName}" is blocked.`);
|
|
1403
|
-
};
|
|
1404
|
-
const sandbox = {
|
|
1405
|
-
require: sandboxRequire,
|
|
1406
|
-
console: { log: () => {
|
|
1407
|
-
}, warn: () => {
|
|
1408
|
-
}, error: () => {
|
|
1409
|
-
} },
|
|
1410
|
-
__filename: "/sandbox/main.ts",
|
|
1411
|
-
__dirname: "/sandbox"
|
|
1412
|
-
};
|
|
1413
|
-
const context = vm.createContext(sandbox);
|
|
1414
|
-
const script = new vm.Script(`
|
|
1415
|
-
((__filename, __dirname) => {
|
|
1416
|
-
let exports = {};
|
|
1417
|
-
let module = { exports };
|
|
1418
|
-
|
|
1419
|
-
${jsCode}
|
|
1420
|
-
|
|
1421
|
-
return module.exports;
|
|
1422
|
-
})("/sandbox/main.ts", "/sandbox");
|
|
1423
|
-
`);
|
|
1424
|
-
const rawExport = script.runInContext(context, {
|
|
1425
|
-
timeout: isDocsMode ? 1e3 : 5e3
|
|
1426
|
-
});
|
|
1427
|
-
const schema = rawExport.default || rawExport;
|
|
1428
|
-
if (!schema || !schema._def) {
|
|
1429
|
-
throw new Error("You must `export default` a valid Zod schema.");
|
|
1430
|
-
}
|
|
1431
|
-
const extractedDef = extractMongooseDef(schema);
|
|
1432
|
-
const mongooseSchema = toMongooseSchema(schema, { modelName: "ParsedSchema" });
|
|
1433
|
-
const formatOpts = { depth: null, showHidden: false, colors: false };
|
|
1434
|
-
const formatValue = (val) => {
|
|
1435
|
-
if (typeof val === "function") {
|
|
1436
|
-
return val.name || val.toString();
|
|
1437
|
-
}
|
|
1438
|
-
if (Array.isArray(val)) {
|
|
1439
|
-
return val.map(formatValue);
|
|
1440
|
-
}
|
|
1441
|
-
if (val !== null && typeof val === "object" && val.constructor === Object) {
|
|
1442
|
-
const formatted = {};
|
|
1443
|
-
for (const key in val) {
|
|
1444
|
-
formatted[key] = formatValue(val[key]);
|
|
1445
|
-
}
|
|
1446
|
-
return formatted;
|
|
1447
|
-
}
|
|
1448
|
-
return val;
|
|
1449
|
-
};
|
|
1450
|
-
const formattedDef = formatValue(extractedDef);
|
|
1451
|
-
const formattedSchema = formatValue(mongooseSchema.obj);
|
|
1452
|
-
const formattedTree = formatValue(mongooseSchema.tree);
|
|
1453
|
-
const formattedPaths = formatValue(mongooseSchema.paths);
|
|
1454
|
-
return {
|
|
1455
|
-
definition: util.inspect(formattedDef, formatOpts),
|
|
1456
|
-
schemaObj: util.inspect(formattedSchema, formatOpts),
|
|
1457
|
-
schemaTree: util.inspect(formattedTree, formatOpts),
|
|
1458
|
-
schemaPaths: util.inspect(formattedPaths, formatOpts)
|
|
1459
|
-
};
|
|
1460
|
-
} catch (err) {
|
|
1461
|
-
const message = getErrorMessage(err);
|
|
1462
|
-
const isCompilationError = (err == null ? void 0 : err.statusCode) === 400;
|
|
1463
|
-
throw createError({
|
|
1464
|
-
statusCode: isCompilationError ? 400 : 500,
|
|
1465
|
-
message: isCompilationError ? message : `Execution Error: ${message}`
|
|
1466
|
-
});
|
|
43
|
+
return executeStudio(body, 5e3);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
throw createError({ statusCode: error instanceof StudioExecutionError ? 400 : 500, message: (error == null ? void 0 : error.message) || "Studio execution failed." });
|
|
1467
46
|
} finally {
|
|
1468
|
-
|
|
1469
|
-
releaseParseLock();
|
|
47
|
+
release();
|
|
1470
48
|
}
|
|
1471
49
|
});
|
|
1472
50
|
|