@nullix/zod-mongoose-studio 2.4.0 → 2.4.1
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/_nuxt/builds/latest.json +1 -1
- package/.output/public/_nuxt/builds/meta/dbb4a1ba-f4e9-417c-85d6-917898095248.json +1 -0
- package/.output/server/chunks/nitro/nitro.mjs +266 -266
- package/.output/server/chunks/routes/api/parse.post.mjs +1018 -1299
- package/.output/server/chunks/routes/api/parse.post.mjs.map +1 -1
- package/.output/server/package.json +1 -1
- package/package.json +2 -2
- package/.output/public/_nuxt/builds/meta/5ffd7935-4f74-4cb6-8566-68adbe0970ba.json +0 -1
|
@@ -5,8 +5,9 @@ import util from 'node:util';
|
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import { dirname } from 'node:path';
|
|
7
7
|
import * as zod from 'zod';
|
|
8
|
-
import * as mongoose
|
|
9
|
-
import
|
|
8
|
+
import * as mongoose from 'mongoose';
|
|
9
|
+
import mongoose__default from 'mongoose';
|
|
10
|
+
import { z } from 'zod/v4';
|
|
10
11
|
import 'node:http';
|
|
11
12
|
import 'node:https';
|
|
12
13
|
import 'node:events';
|
|
@@ -16,1395 +17,1113 @@ import 'node:crypto';
|
|
|
16
17
|
import '@iconify/utils';
|
|
17
18
|
import 'consola';
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
} }
|
|
33
|
-
}
|
|
34
|
-
return Object.freeze(Object.defineProperty(n, Symbol.toStringTag, { value: 'Module' }));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function getDefaultExportFromNamespaceIfNotNamed (n) {
|
|
38
|
-
return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
var dist = {};
|
|
42
|
-
|
|
43
|
-
const require$$0 = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(v4$1);
|
|
44
|
-
|
|
45
|
-
const require$$1 = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(mongoose$1);
|
|
46
|
-
|
|
47
|
-
var v4 = require$$0;
|
|
48
|
-
var mongoose = require$$1;
|
|
49
|
-
|
|
50
|
-
//#region src/utils.ts
|
|
51
|
-
function flatHooks(configHooks, hooks = {}, parentName) {
|
|
52
|
-
for (const key in configHooks) {
|
|
53
|
-
const subHook = configHooks[key];
|
|
54
|
-
const name = parentName ? `${parentName}:${key}` : key;
|
|
55
|
-
if (typeof subHook === "object" && subHook !== null) flatHooks(subHook, hooks, name);
|
|
56
|
-
else if (typeof subHook === "function") hooks[name] = subHook;
|
|
57
|
-
}
|
|
58
|
-
return hooks;
|
|
20
|
+
var __defProp = Object.defineProperty;
|
|
21
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
22
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
23
|
+
function flatHooks(configHooks, hooks2 = {}, parentName) {
|
|
24
|
+
for (const key in configHooks) {
|
|
25
|
+
const subHook = configHooks[key];
|
|
26
|
+
const name = parentName ? `${parentName}:${key}` : key;
|
|
27
|
+
if (typeof subHook === "object" && subHook !== null) flatHooks(subHook, hooks2, name);
|
|
28
|
+
else if (typeof subHook === "function") hooks2[name] = subHook;
|
|
29
|
+
}
|
|
30
|
+
return hooks2;
|
|
59
31
|
}
|
|
60
32
|
const createTask = /* @__PURE__ */ (() => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
33
|
+
if (console.createTask) return console.createTask;
|
|
34
|
+
const defaultTask = { run: (fn) => fn() };
|
|
35
|
+
return () => defaultTask;
|
|
64
36
|
})();
|
|
65
|
-
function callHooks(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
37
|
+
function callHooks(hooks2, args, startIndex, task) {
|
|
38
|
+
for (let i = startIndex; i < hooks2.length; i += 1) try {
|
|
39
|
+
const result = task ? task.run(() => hooks2[i](...args)) : hooks2[i](...args);
|
|
40
|
+
if (result instanceof Promise) return result.then(() => callHooks(hooks2, args, i + 1, task));
|
|
41
|
+
} catch (error) {
|
|
42
|
+
return Promise.reject(error);
|
|
43
|
+
}
|
|
72
44
|
}
|
|
73
|
-
function serialTaskCaller(
|
|
74
|
-
|
|
45
|
+
function serialTaskCaller(hooks2, args, name) {
|
|
46
|
+
if (hooks2.length > 0) return callHooks(hooks2, args, 0, createTask(name));
|
|
75
47
|
}
|
|
76
|
-
function parallelTaskCaller(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
48
|
+
function parallelTaskCaller(hooks2, args, name) {
|
|
49
|
+
if (hooks2.length > 0) {
|
|
50
|
+
const task = createTask(name);
|
|
51
|
+
return Promise.all(hooks2.map((hook) => task.run(() => hook(...args))));
|
|
52
|
+
}
|
|
81
53
|
}
|
|
82
54
|
function callEachWith(callbacks, arg0) {
|
|
83
|
-
|
|
55
|
+
for (const callback of [...callbacks]) callback(arg0);
|
|
84
56
|
}
|
|
85
|
-
//#endregion
|
|
86
|
-
//#region src/hookable.ts
|
|
87
57
|
var Hookable = class {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
58
|
+
constructor() {
|
|
59
|
+
__publicField(this, "_hooks");
|
|
60
|
+
__publicField(this, "_before");
|
|
61
|
+
__publicField(this, "_after");
|
|
62
|
+
__publicField(this, "_deprecatedHooks");
|
|
63
|
+
__publicField(this, "_deprecatedMessages");
|
|
64
|
+
this._hooks = {};
|
|
65
|
+
this._before = void 0;
|
|
66
|
+
this._after = void 0;
|
|
67
|
+
this._deprecatedMessages = void 0;
|
|
68
|
+
this._deprecatedHooks = {};
|
|
69
|
+
this.hook = this.hook.bind(this);
|
|
70
|
+
this.callHook = this.callHook.bind(this);
|
|
71
|
+
this.callHookWith = this.callHookWith.bind(this);
|
|
72
|
+
}
|
|
73
|
+
hook(name, function_, options = {}) {
|
|
74
|
+
if (!name || typeof function_ !== "function") return () => {
|
|
75
|
+
};
|
|
76
|
+
const originalName = name;
|
|
77
|
+
let dep;
|
|
78
|
+
while (this._deprecatedHooks[name]) {
|
|
79
|
+
dep = this._deprecatedHooks[name];
|
|
80
|
+
name = dep.to;
|
|
81
|
+
}
|
|
82
|
+
if (dep && !options.allowDeprecated) {
|
|
83
|
+
let message = dep.message;
|
|
84
|
+
if (!message) message = `${originalName} hook has been deprecated` + (dep.to ? `, please use ${dep.to}` : "");
|
|
85
|
+
if (!this._deprecatedMessages) this._deprecatedMessages = /* @__PURE__ */ new Set();
|
|
86
|
+
if (!this._deprecatedMessages.has(message)) {
|
|
87
|
+
console.warn(message);
|
|
88
|
+
this._deprecatedMessages.add(message);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!function_.name) try {
|
|
92
|
+
Object.defineProperty(function_, "name", {
|
|
93
|
+
get: () => "_" + name.replace(/\W+/g, "_") + "_hook_cb",
|
|
94
|
+
configurable: true
|
|
95
|
+
});
|
|
96
|
+
} catch {
|
|
97
|
+
}
|
|
98
|
+
this._hooks[name] = this._hooks[name] || [];
|
|
99
|
+
this._hooks[name].push(function_);
|
|
100
|
+
return () => {
|
|
101
|
+
if (function_) {
|
|
102
|
+
this.removeHook(name, function_);
|
|
103
|
+
function_ = void 0;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
hookOnce(name, function_) {
|
|
108
|
+
let _unreg;
|
|
109
|
+
let _function = (...arguments_) => {
|
|
110
|
+
if (typeof _unreg === "function") _unreg();
|
|
111
|
+
_unreg = void 0;
|
|
112
|
+
_function = void 0;
|
|
113
|
+
return function_(...arguments_);
|
|
114
|
+
};
|
|
115
|
+
_unreg = this.hook(name, _function);
|
|
116
|
+
return _unreg;
|
|
117
|
+
}
|
|
118
|
+
removeHook(name, function_) {
|
|
119
|
+
const hooks2 = this._hooks[name];
|
|
120
|
+
if (hooks2) {
|
|
121
|
+
const index = hooks2.indexOf(function_);
|
|
122
|
+
if (index !== -1) hooks2.splice(index, 1);
|
|
123
|
+
if (hooks2.length === 0) this._hooks[name] = void 0;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
clearHook(name) {
|
|
127
|
+
this._hooks[name] = void 0;
|
|
128
|
+
}
|
|
129
|
+
deprecateHook(name, deprecated) {
|
|
130
|
+
this._deprecatedHooks[name] = typeof deprecated === "string" ? { to: deprecated } : deprecated;
|
|
131
|
+
const _hooks = this._hooks[name] || [];
|
|
132
|
+
this._hooks[name] = void 0;
|
|
133
|
+
for (const hook of _hooks) this.hook(name, hook);
|
|
134
|
+
}
|
|
135
|
+
deprecateHooks(deprecatedHooks) {
|
|
136
|
+
for (const name in deprecatedHooks) this.deprecateHook(name, deprecatedHooks[name]);
|
|
137
|
+
}
|
|
138
|
+
addHooks(configHooks) {
|
|
139
|
+
const hooks2 = flatHooks(configHooks);
|
|
140
|
+
const removeFns = Object.keys(hooks2).map((key) => this.hook(key, hooks2[key]));
|
|
141
|
+
return () => {
|
|
142
|
+
for (const unreg of removeFns) unreg();
|
|
143
|
+
removeFns.length = 0;
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
removeHooks(configHooks) {
|
|
147
|
+
const hooks2 = flatHooks(configHooks);
|
|
148
|
+
for (const key in hooks2) this.removeHook(key, hooks2[key]);
|
|
149
|
+
}
|
|
150
|
+
removeAllHooks() {
|
|
151
|
+
this._hooks = {};
|
|
152
|
+
}
|
|
153
|
+
callHook(name, ...args) {
|
|
154
|
+
return this.callHookWith(serialTaskCaller, name, args);
|
|
155
|
+
}
|
|
156
|
+
callHookParallel(name, ...args) {
|
|
157
|
+
return this.callHookWith(parallelTaskCaller, name, args);
|
|
158
|
+
}
|
|
159
|
+
callHookWith(caller, name, args) {
|
|
160
|
+
const event = this._before || this._after ? {
|
|
161
|
+
name,
|
|
162
|
+
args,
|
|
163
|
+
context: {}
|
|
164
|
+
} : void 0;
|
|
165
|
+
if (this._before) callEachWith(this._before, event);
|
|
166
|
+
const result = caller(this._hooks[name] ? [...this._hooks[name]] : [], args, name);
|
|
167
|
+
if (result instanceof Promise) return result.finally(() => {
|
|
168
|
+
if (this._after && event) callEachWith(this._after, event);
|
|
169
|
+
});
|
|
170
|
+
if (this._after && event) callEachWith(this._after, event);
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
beforeEach(function_) {
|
|
174
|
+
this._before = this._before || [];
|
|
175
|
+
this._before.push(function_);
|
|
176
|
+
return () => {
|
|
177
|
+
if (this._before !== void 0) {
|
|
178
|
+
const index = this._before.indexOf(function_);
|
|
179
|
+
if (index !== -1) this._before.splice(index, 1);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
afterEach(function_) {
|
|
184
|
+
this._after = this._after || [];
|
|
185
|
+
this._after.push(function_);
|
|
186
|
+
return () => {
|
|
187
|
+
if (this._after !== void 0) {
|
|
188
|
+
const index = this._after.indexOf(function_);
|
|
189
|
+
if (index !== -1) this._after.splice(index, 1);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
221
193
|
};
|
|
222
194
|
function createHooks() {
|
|
223
|
-
|
|
195
|
+
return new Hookable();
|
|
224
196
|
}
|
|
225
|
-
|
|
226
197
|
const hooks = createHooks();
|
|
227
|
-
/**
|
|
228
|
-
* Synchronous hook caller for Hookable.
|
|
229
|
-
*/
|
|
230
198
|
function callHookSync(name, ...args) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
199
|
+
hooks.callHookWith((callbacks, args2) => {
|
|
200
|
+
for (const callback of callbacks) {
|
|
201
|
+
callback(...args2);
|
|
202
|
+
}
|
|
203
|
+
}, name, args);
|
|
236
204
|
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* This securely stores our Mongoose metadata alongside the Zod schema instances
|
|
240
|
-
* without polluting the actual validation logic.
|
|
241
|
-
*/
|
|
242
|
-
const mongooseRegistry = v4.z.registry();
|
|
243
|
-
/**
|
|
244
|
-
* A clean wrapper to attach Mongoose metadata to any Zod schema.
|
|
245
|
-
*/
|
|
205
|
+
const mongooseRegistry = z.registry();
|
|
246
206
|
function withMongoose(schema, meta = {}) {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
207
|
+
callHookSync("registry:get:before", { schema });
|
|
208
|
+
const existing = mongooseRegistry.get(schema) || {};
|
|
209
|
+
callHookSync("registry:get", { schema, meta: existing });
|
|
210
|
+
const merged = { ...existing, ...meta };
|
|
211
|
+
callHookSync("registry:add", { schema, meta: merged });
|
|
212
|
+
mongooseRegistry.add(schema, merged);
|
|
213
|
+
callHookSync("registry:added", { schema, meta: merged });
|
|
214
|
+
return schema;
|
|
255
215
|
}
|
|
256
|
-
/**
|
|
257
|
-
* Recursively collect Mongoose metadata from a Zod schema and its wrappers.
|
|
258
|
-
*/
|
|
259
216
|
function getMongooseMeta(schema) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
// Collect from both 'in' and 'out' parts, preferring metadata from 'out' if it exists,
|
|
274
|
-
// but the pipe itself usually holds the metadata we want.
|
|
275
|
-
meta = { ...getMongooseMeta(def.in), ...getMongooseMeta(def.out), ...meta };
|
|
276
|
-
}
|
|
277
|
-
return meta;
|
|
217
|
+
const def = schema._def;
|
|
218
|
+
if (!def)
|
|
219
|
+
return {};
|
|
220
|
+
let meta = mongooseRegistry.get(schema) || {};
|
|
221
|
+
if (def.innerType) {
|
|
222
|
+
meta = { ...getMongooseMeta(def.innerType), ...meta };
|
|
223
|
+
} else if (def.schema) {
|
|
224
|
+
meta = { ...getMongooseMeta(def.schema), ...meta };
|
|
225
|
+
}
|
|
226
|
+
if (def.type === "pipe") {
|
|
227
|
+
meta = { ...getMongooseMeta(def.in), ...getMongooseMeta(def.out), ...meta };
|
|
228
|
+
}
|
|
229
|
+
return meta;
|
|
278
230
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
// For transform: in = schema, out = transformation
|
|
329
|
-
// For preprocess: in = preprocessing, out = schema
|
|
330
|
-
if (type === 'pipe') {
|
|
331
|
-
const inType = def.in?._def?.type;
|
|
332
|
-
const outType = def.out?._def?.type;
|
|
333
|
-
if (inType === 'transform') {
|
|
334
|
-
// It's a preprocess (in is transformation, out is schema)
|
|
335
|
-
return unwrapZodSchema(def.out, features, visited);
|
|
336
|
-
}
|
|
337
|
-
if (outType === 'transform' || outType === 'refinement') {
|
|
338
|
-
// It's a transform or refine (in is schema, out is logic)
|
|
339
|
-
// We should still collect transformations from the 'out' part
|
|
340
|
-
const transformFeatures = { ...features };
|
|
341
|
-
const outDef = def.out?._def;
|
|
342
|
-
const effects = outDef?.effects || outDef?.transformations || (outType === 'transform' ? [outDef] : []);
|
|
343
|
-
if (effects && Array.isArray(effects)) {
|
|
344
|
-
transformFeatures.transformations = [
|
|
345
|
-
...(transformFeatures.transformations || []),
|
|
346
|
-
...effects,
|
|
347
|
-
];
|
|
348
|
-
}
|
|
349
|
-
return unwrapZodSchema(def.in, transformFeatures, visited);
|
|
350
|
-
}
|
|
351
|
-
// Default pipe behavior (extract the output part)
|
|
352
|
-
return unwrapZodSchema(def.out, features, visited);
|
|
353
|
-
}
|
|
354
|
-
if (type === 'transform' ||
|
|
355
|
-
type === 'preprocess' ||
|
|
356
|
-
type === 'refinement' ||
|
|
357
|
-
type === 'effects') {
|
|
358
|
-
const transformFeatures = { ...features };
|
|
359
|
-
const effects = def.effects || def.transformations || (def.type === 'transform' || type === 'transform' ? [def] : []);
|
|
360
|
-
if (effects && Array.isArray(effects)) {
|
|
361
|
-
transformFeatures.transformations = [
|
|
362
|
-
...(transformFeatures.transformations || []),
|
|
363
|
-
...effects,
|
|
364
|
-
];
|
|
365
|
-
}
|
|
366
|
-
const inner = def.schema || def.innerType;
|
|
367
|
-
if (inner) {
|
|
368
|
-
const result = unwrapZodSchema(inner, transformFeatures, visited);
|
|
369
|
-
return result;
|
|
370
|
-
}
|
|
231
|
+
function unwrapZodSchema(schema, features = { required: true }, visited = /* @__PURE__ */ new Set()) {
|
|
232
|
+
var _a, _b, _c, _d, _e;
|
|
233
|
+
if (!schema)
|
|
234
|
+
return { schema, features };
|
|
235
|
+
if (visited.has(schema))
|
|
236
|
+
return { schema, features };
|
|
237
|
+
const def = schema._def;
|
|
238
|
+
if (!def)
|
|
239
|
+
return { schema, features };
|
|
240
|
+
if (!(schema instanceof z.ZodOptional) && !(schema instanceof z.ZodNullable) && !(schema instanceof z.ZodDefault) && def.type !== "pipe") {
|
|
241
|
+
visited.add(schema);
|
|
242
|
+
}
|
|
243
|
+
if (schema instanceof z.ZodOptional) {
|
|
244
|
+
const inner = schema.unwrap();
|
|
245
|
+
return unwrapZodSchema(
|
|
246
|
+
// @ts-expect-error Zod v4 schema.unwrap() return type mismatch
|
|
247
|
+
inner,
|
|
248
|
+
{
|
|
249
|
+
...features,
|
|
250
|
+
required: false,
|
|
251
|
+
isOptional: true
|
|
252
|
+
},
|
|
253
|
+
visited
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
if (schema instanceof z.ZodNullable) {
|
|
257
|
+
return unwrapZodSchema(
|
|
258
|
+
// @ts-expect-error Zod v4 schema.unwrap() return type mismatch
|
|
259
|
+
schema.unwrap(),
|
|
260
|
+
{
|
|
261
|
+
...features,
|
|
262
|
+
isNullable: true
|
|
263
|
+
},
|
|
264
|
+
visited
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
if (schema instanceof z.ZodDefault) {
|
|
268
|
+
const defaultValue = typeof def.defaultValue === "function" ? def.defaultValue() : def.defaultValue;
|
|
269
|
+
return unwrapZodSchema(def.innerType, {
|
|
270
|
+
...features,
|
|
271
|
+
default: defaultValue
|
|
272
|
+
}, visited);
|
|
273
|
+
}
|
|
274
|
+
const { type } = def;
|
|
275
|
+
if (type === "pipe") {
|
|
276
|
+
const inType = (_b = (_a = def.in) == null ? void 0 : _a._def) == null ? void 0 : _b.type;
|
|
277
|
+
const outType = (_d = (_c = def.out) == null ? void 0 : _c._def) == null ? void 0 : _d.type;
|
|
278
|
+
if (inType === "transform") {
|
|
279
|
+
return unwrapZodSchema(def.out, features, visited);
|
|
371
280
|
}
|
|
372
|
-
if (
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
281
|
+
if (outType === "transform" || outType === "refinement") {
|
|
282
|
+
const transformFeatures = { ...features };
|
|
283
|
+
const outDef = (_e = def.out) == null ? void 0 : _e._def;
|
|
284
|
+
const effects = (outDef == null ? void 0 : outDef.effects) || (outDef == null ? void 0 : outDef.transformations) || (outType === "transform" ? [outDef] : []);
|
|
285
|
+
if (effects && Array.isArray(effects)) {
|
|
286
|
+
transformFeatures.transformations = [
|
|
287
|
+
...transformFeatures.transformations || [],
|
|
288
|
+
...effects
|
|
289
|
+
];
|
|
290
|
+
}
|
|
291
|
+
return unwrapZodSchema(def.in, transformFeatures, visited);
|
|
378
292
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
293
|
+
return unwrapZodSchema(def.out, features, visited);
|
|
294
|
+
}
|
|
295
|
+
if (type === "transform" || type === "preprocess" || type === "refinement" || type === "effects") {
|
|
296
|
+
const transformFeatures = { ...features };
|
|
297
|
+
const effects = def.effects || def.transformations || (def.type === "transform" || type === "transform" ? [def] : []);
|
|
298
|
+
if (effects && Array.isArray(effects)) {
|
|
299
|
+
transformFeatures.transformations = [
|
|
300
|
+
...transformFeatures.transformations || [],
|
|
301
|
+
...effects
|
|
302
|
+
];
|
|
384
303
|
}
|
|
385
|
-
|
|
386
|
-
if (
|
|
387
|
-
|
|
304
|
+
const inner = def.schema || def.innerType;
|
|
305
|
+
if (inner) {
|
|
306
|
+
const result = unwrapZodSchema(inner, transformFeatures, visited);
|
|
307
|
+
return result;
|
|
388
308
|
}
|
|
309
|
+
}
|
|
310
|
+
if (type === "lazy") {
|
|
389
311
|
return { schema, features };
|
|
312
|
+
}
|
|
313
|
+
if (type === "branded" || type === "readonly") {
|
|
314
|
+
return unwrapZodSchema(schema.unwrap(), {
|
|
315
|
+
...features,
|
|
316
|
+
...type === "readonly" ? { readOnly: true } : {}
|
|
317
|
+
}, visited);
|
|
318
|
+
}
|
|
319
|
+
if (def.checks && Array.isArray(def.checks)) {
|
|
320
|
+
features.checks = [...features.checks || [], ...def.checks];
|
|
321
|
+
}
|
|
322
|
+
return { schema, features };
|
|
390
323
|
}
|
|
391
|
-
|
|
392
324
|
let mongooseInstance = null;
|
|
393
|
-
/**
|
|
394
|
-
* Manually set the Mongoose instance.
|
|
395
|
-
* Useful in ESM environments where automatic detection via require() might fail.
|
|
396
|
-
*/
|
|
397
325
|
const setMongoose = (m) => {
|
|
398
|
-
|
|
326
|
+
mongooseInstance = m;
|
|
399
327
|
};
|
|
400
|
-
// Helper to get mongoose instance safely
|
|
401
328
|
const getMongoose = () => {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
}
|
|
411
|
-
return m;
|
|
329
|
+
var _a;
|
|
330
|
+
if (mongooseInstance) {
|
|
331
|
+
return mongooseInstance;
|
|
332
|
+
}
|
|
333
|
+
try {
|
|
334
|
+
const m = require("mongoose");
|
|
335
|
+
if (m && (m.Schema || ((_a = m.default) == null ? void 0 : _a.Schema))) {
|
|
336
|
+
return m.default || m;
|
|
412
337
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
return null;
|
|
338
|
+
return m;
|
|
339
|
+
} catch {
|
|
340
|
+
if (globalThis.mongoose) {
|
|
341
|
+
return globalThis.mongoose;
|
|
419
342
|
}
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
420
345
|
};
|
|
421
346
|
let isFrontend = false;
|
|
422
|
-
/**
|
|
423
|
-
* Enable or disable frontend mode.
|
|
424
|
-
* In frontend mode, specialized types like ObjectId and Buffer fall back to
|
|
425
|
-
* simpler representations (strings/arrays) and do not depend on Mongoose.
|
|
426
|
-
*/
|
|
427
347
|
const setFrontendMode = (enabled) => {
|
|
428
|
-
|
|
348
|
+
isFrontend = enabled;
|
|
429
349
|
};
|
|
430
350
|
const getFrontendMode = () => {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
}
|
|
436
|
-
return isFrontend;
|
|
351
|
+
if (isFrontend === void 0 || isFrontend === null) {
|
|
352
|
+
return globalThis.window !== void 0 && globalThis.document !== void 0;
|
|
353
|
+
}
|
|
354
|
+
return isFrontend;
|
|
437
355
|
};
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* Helper to map Zod checks (min, max, regex, etc.) to Mongoose Schema properties.
|
|
441
|
-
*/
|
|
442
356
|
function mapZodChecksToMongoose(checks, mongooseProp) {
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
}
|
|
454
|
-
if (traitSet.has('$ZodCheckMaxLength')) {
|
|
455
|
-
mongooseProp.maxlength = checkDef.maximum;
|
|
456
|
-
}
|
|
457
|
-
if (traitSet.has('$ZodCheckLengthEquals')) {
|
|
458
|
-
mongooseProp.minlength = checkDef.length;
|
|
459
|
-
mongooseProp.maxlength = checkDef.length;
|
|
460
|
-
}
|
|
461
|
-
// Numbers and Dates Comparisons
|
|
462
|
-
if (traitSet.has('$ZodCheckGreaterThan')) {
|
|
463
|
-
mongooseProp.min = checkDef.value;
|
|
464
|
-
}
|
|
465
|
-
if (traitSet.has('$ZodCheckLessThan')) {
|
|
466
|
-
mongooseProp.max = checkDef.value;
|
|
467
|
-
}
|
|
468
|
-
// Regex / Match
|
|
469
|
-
if (traitSet.has('$ZodCheckRegex')) {
|
|
470
|
-
mongooseProp.match = checkDef.pattern;
|
|
471
|
-
}
|
|
472
|
-
// UUID
|
|
473
|
-
if (traitSet.has('$ZodUUID')) {
|
|
474
|
-
const mongoose = globalThis.mongoose || globalThis.__mongoose;
|
|
475
|
-
if (mongoose?.Schema.Types.UUID) {
|
|
476
|
-
mongooseProp.type = mongoose.Schema.Types.UUID;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
// ISO Formats
|
|
480
|
-
if (traitSet.has('$ZodISODateTime') || traitSet.has('$ZodISODate')) {
|
|
481
|
-
mongooseProp.type = Date;
|
|
482
|
-
}
|
|
483
|
-
// String Transforms (trim, lowercase, uppercase)
|
|
484
|
-
if (traitSet.has('$ZodCheckOverwrite') && typeof checkDef.tx === 'function') {
|
|
485
|
-
const txStr = checkDef.tx.toString();
|
|
486
|
-
if (txStr.includes('.trim()')) {
|
|
487
|
-
mongooseProp.trim = true;
|
|
488
|
-
}
|
|
489
|
-
else if (txStr.includes('.toLowerCase()')) {
|
|
490
|
-
mongooseProp.lowercase = true;
|
|
491
|
-
}
|
|
492
|
-
else if (txStr.includes('.toUpperCase()')) {
|
|
493
|
-
mongooseProp.uppercase = true;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
357
|
+
var _a, _b;
|
|
358
|
+
if (!checks || !Array.isArray(checks))
|
|
359
|
+
return;
|
|
360
|
+
for (const check of checks) {
|
|
361
|
+
const traitSet = (_a = check._zod) == null ? void 0 : _a.traits;
|
|
362
|
+
const checkDef = (_b = check._zod) == null ? void 0 : _b.def;
|
|
363
|
+
if (!traitSet || !checkDef)
|
|
364
|
+
continue;
|
|
365
|
+
if (traitSet.has("$ZodCheckMinLength")) {
|
|
366
|
+
mongooseProp.minlength = checkDef.minimum;
|
|
496
367
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* Handles ZodObject conversion to Mongoose Schema definition.
|
|
502
|
-
*/
|
|
503
|
-
function handleObject(unwrapped, mongooseProp, visited, extractMongooseDef, isField = false) {
|
|
504
|
-
callHookSync('schema:object:before', { schema: unwrapped, mongooseProp, visited });
|
|
505
|
-
const { shape } = unwrapped;
|
|
506
|
-
const objDef = {};
|
|
507
|
-
// We must ensure recursive calls see the current object to break cycles.
|
|
508
|
-
const placeholder = mongooseProp.type ? mongooseProp : objDef;
|
|
509
|
-
visited.set(unwrapped, placeholder);
|
|
510
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
511
|
-
for (const key in shape) {
|
|
512
|
-
if (!Object.prototype.hasOwnProperty.call(shape, key))
|
|
513
|
-
continue;
|
|
514
|
-
// Skip automatic _id mapping unless explicitly requested
|
|
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 &&
|
|
520
|
-
unwrappedIdMeta.includeId !== true &&
|
|
521
|
-
mongooseProp.includeId !== true) {
|
|
522
|
-
continue;
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
const def = extractMongooseDef(shape[key], visited);
|
|
526
|
-
if (def && typeof def === 'object' && def.__isDiscriminatorUnion) {
|
|
527
|
-
const mongoose = getMongoose();
|
|
528
|
-
if (mongoose) {
|
|
529
|
-
const baseSchema = new mongoose.Schema(def.baseDef, {
|
|
530
|
-
discriminatorKey: def.discriminatorKey,
|
|
531
|
-
_id: false,
|
|
532
|
-
});
|
|
533
|
-
const discriminators = {};
|
|
534
|
-
for (const [dKey, dDef] of Object.entries(def.discriminators)) {
|
|
535
|
-
discriminators[dKey] = new mongoose.Schema(dDef, { _id: false });
|
|
536
|
-
}
|
|
537
|
-
objDef[key] = {
|
|
538
|
-
type: baseSchema,
|
|
539
|
-
discriminators,
|
|
540
|
-
};
|
|
541
|
-
}
|
|
542
|
-
else {
|
|
543
|
-
objDef[key] = def;
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
else if (typeof def === 'object' && def !== null && !Array.isArray(def)) {
|
|
547
|
-
const { includeId, ...cleanDef } = def;
|
|
548
|
-
objDef[key] = cleanDef;
|
|
549
|
-
}
|
|
550
|
-
else {
|
|
551
|
-
objDef[key] = def;
|
|
552
|
-
}
|
|
553
|
-
callHookSync('schema:object:field', { key, schema: shape[key], objDef, visited });
|
|
368
|
+
if (traitSet.has("$ZodCheckMaxLength")) {
|
|
369
|
+
mongooseProp.maxlength = checkDef.maximum;
|
|
554
370
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
const shouldBeSubSchema = isField && mongooseProp.schema !== false;
|
|
559
|
-
if (shouldBeSubSchema && !mongooseProp.type) {
|
|
560
|
-
const mongoose = getMongoose();
|
|
561
|
-
if (mongoose) {
|
|
562
|
-
const options = typeof mongooseProp.schema === 'object' ? mongooseProp.schema : {};
|
|
563
|
-
const { plugins, ...schemaOptions } = options;
|
|
564
|
-
const subSchema = new mongoose.Schema(objDef, schemaOptions);
|
|
565
|
-
if (plugins && Array.isArray(plugins)) {
|
|
566
|
-
for (const plugin of plugins) {
|
|
567
|
-
subSchema.plugin(plugin);
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
mongooseProp.type = subSchema;
|
|
571
|
-
}
|
|
371
|
+
if (traitSet.has("$ZodCheckLengthEquals")) {
|
|
372
|
+
mongooseProp.minlength = checkDef.length;
|
|
373
|
+
mongooseProp.maxlength = checkDef.length;
|
|
572
374
|
}
|
|
573
|
-
if (
|
|
574
|
-
|
|
575
|
-
const isSchema = mongoose && (mongooseProp.type instanceof mongoose.Schema || mongooseProp.type.constructor?.name === 'Schema');
|
|
576
|
-
// If there is a type override, merge the object definition into the result unless it's a Schema
|
|
577
|
-
if (!isSchema) {
|
|
578
|
-
Object.assign(mongooseProp, objDef);
|
|
579
|
-
}
|
|
580
|
-
result = mongooseProp;
|
|
375
|
+
if (traitSet.has("$ZodCheckGreaterThan")) {
|
|
376
|
+
mongooseProp.min = checkDef.value;
|
|
581
377
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
const topLevelOptions = 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;
|
|
378
|
+
if (traitSet.has("$ZodCheckLessThan")) {
|
|
379
|
+
mongooseProp.max = checkDef.value;
|
|
606
380
|
}
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
}
|
|
610
|
-
/**
|
|
611
|
-
* Handles ZodArray, ZodSet, and ZodTuple conversion.
|
|
612
|
-
*/
|
|
613
|
-
function handleArray(unwrapped, mongooseProp, visited, extractMongooseDef) {
|
|
614
|
-
callHookSync('schema:array:before', { schema: unwrapped, mongooseProp, visited });
|
|
615
|
-
const element = unwrapped.element ||
|
|
616
|
-
unwrapped._def.valueType ||
|
|
617
|
-
unwrapped._def.rest ||
|
|
618
|
-
unwrapped._def.items?.[0];
|
|
619
|
-
const mongoose = getMongoose();
|
|
620
|
-
const innerDef = element
|
|
621
|
-
? extractMongooseDef(element, visited)
|
|
622
|
-
: mongoose?.Schema.Types.Mixed || 'Mixed';
|
|
623
|
-
// If no explicit type override, wrap the inner definition in an array
|
|
624
|
-
if (!mongooseProp.type) {
|
|
625
|
-
if (innerDef && typeof innerDef === 'object' && innerDef.__isDiscriminatorUnion && mongoose) {
|
|
626
|
-
// const baseSchema =
|
|
627
|
-
// new mongoose.Schema(innerDef.baseDef, {
|
|
628
|
-
// discriminatorKey: innerDef.discriminatorKey,
|
|
629
|
-
// _id: false,
|
|
630
|
-
// });
|
|
631
|
-
const discriminators = {};
|
|
632
|
-
for (const [dKey, dDef] of Object.entries(innerDef.discriminators)) {
|
|
633
|
-
discriminators[dKey] = new mongoose.Schema(dDef, { _id: false });
|
|
634
|
-
}
|
|
635
|
-
mongooseProp.type = [
|
|
636
|
-
new mongoose.Schema({}, {
|
|
637
|
-
discriminatorKey: innerDef.discriminatorKey,
|
|
638
|
-
_id: false,
|
|
639
|
-
}),
|
|
640
|
-
];
|
|
641
|
-
mongooseProp.discriminators = discriminators;
|
|
642
|
-
}
|
|
643
|
-
else {
|
|
644
|
-
const innerType = innerDef.type || innerDef;
|
|
645
|
-
mongooseProp.type = [innerType];
|
|
646
|
-
// Transfer any metadata from the inner type (like 'ref') to the array definition
|
|
647
|
-
if (typeof innerDef === 'object') {
|
|
648
|
-
// eslint-disable-next-line sonarjs/no-unused-vars
|
|
649
|
-
const { type: _extractedType, ...innerMeta } = innerDef;
|
|
650
|
-
Object.assign(mongooseProp, innerMeta);
|
|
651
|
-
mongooseProp.type = [innerType]; // Restore type as array
|
|
652
|
-
}
|
|
653
|
-
}
|
|
381
|
+
if (traitSet.has("$ZodCheckRegex")) {
|
|
382
|
+
mongooseProp.match = checkDef.pattern;
|
|
654
383
|
}
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
function handleRecord(unwrapped, mongooseProp, visited, extractMongooseDef) {
|
|
661
|
-
callHookSync('schema:record:before', { schema: unwrapped, mongooseProp, visited });
|
|
662
|
-
const type = unwrapped._def?.type;
|
|
663
|
-
const isMap = type === 'map';
|
|
664
|
-
const valueType = unwrapped.valueType ||
|
|
665
|
-
unwrapped.valueSchema ||
|
|
666
|
-
unwrapped._def.valueType ||
|
|
667
|
-
unwrapped._def.valueSchema ||
|
|
668
|
-
unwrapped._def.innerType; // For some Zod versions
|
|
669
|
-
let innerDef;
|
|
670
|
-
if (!mongooseProp.type || mongooseProp.type === Map || mongooseProp.type === Object) {
|
|
671
|
-
// z.record() maps to a POJO (Object/Mixed), while z.map() maps to a Mongoose Map
|
|
672
|
-
mongooseProp.type = isMap ? Map : Object;
|
|
673
|
-
const finalValueType = valueType || unwrapped.valueSchema || unwrapped._def?.valueSchema;
|
|
674
|
-
if (finalValueType) {
|
|
675
|
-
innerDef = extractMongooseDef(finalValueType, visited);
|
|
676
|
-
mongooseProp.of = innerDef.type || innerDef;
|
|
677
|
-
}
|
|
384
|
+
if (traitSet.has("$ZodUUID")) {
|
|
385
|
+
const mongoose2 = globalThis.mongoose || globalThis.__mongoose;
|
|
386
|
+
if (mongoose2 == null ? void 0 : mongoose2.Schema.Types.UUID) {
|
|
387
|
+
mongooseProp.type = mongoose2.Schema.Types.UUID;
|
|
388
|
+
}
|
|
678
389
|
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
/**
|
|
683
|
-
* THE CONVERTER (Safe AST Walker)
|
|
684
|
-
* We extract the Zod type and merge it with any registered Mongoose metadata.
|
|
685
|
-
*/
|
|
686
|
-
function extractMongooseDef(schema, visited = new Map(), isField = false, noWrap = false) {
|
|
687
|
-
// Only call converter:before at the very beginning of a run
|
|
688
|
-
if (visited.size === 0) {
|
|
689
|
-
callHookSync('converter:before', { schema: schema, visited });
|
|
390
|
+
if (traitSet.has("$ZodISODateTime") || traitSet.has("$ZodISODate")) {
|
|
391
|
+
mongooseProp.type = Date;
|
|
690
392
|
}
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
meta: mongooseProp,
|
|
701
|
-
mongooseProp: mongooseProp,
|
|
702
|
-
});
|
|
703
|
-
if (features.isOptional === true && mongooseProp.type && mongooseProp.required !== true) {
|
|
704
|
-
mongooseProp.required = false;
|
|
393
|
+
if (traitSet.has("$ZodCheckOverwrite") && typeof checkDef.tx === "function") {
|
|
394
|
+
const txStr = checkDef.tx.toString();
|
|
395
|
+
if (txStr.includes(".trim()")) {
|
|
396
|
+
mongooseProp.trim = true;
|
|
397
|
+
} else if (txStr.includes(".toLowerCase()")) {
|
|
398
|
+
mongooseProp.lowercase = true;
|
|
399
|
+
} else if (txStr.includes(".toUpperCase()")) {
|
|
400
|
+
mongooseProp.uppercase = true;
|
|
401
|
+
}
|
|
705
402
|
}
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
403
|
+
}
|
|
404
|
+
callHookSync("validation:mappers", { checks, mongooseProp });
|
|
405
|
+
}
|
|
406
|
+
function handleObject(unwrapped, mongooseProp, visited, extractMongooseDef2, isField = false) {
|
|
407
|
+
var _a;
|
|
408
|
+
callHookSync("schema:object:before", { schema: unwrapped, mongooseProp, visited });
|
|
409
|
+
const { shape } = unwrapped;
|
|
410
|
+
const objDef = {};
|
|
411
|
+
const placeholder = mongooseProp.type ? mongooseProp : objDef;
|
|
412
|
+
visited.set(unwrapped, placeholder);
|
|
413
|
+
for (const key in shape) {
|
|
414
|
+
if (!Object.prototype.hasOwnProperty.call(shape, key))
|
|
415
|
+
continue;
|
|
416
|
+
if (key === "_id") {
|
|
417
|
+
const idMeta = mongooseRegistry.get(shape[key]) || {};
|
|
418
|
+
const unwrappedId = unwrapZodSchema(shape[key]).schema;
|
|
419
|
+
const unwrappedIdMeta = mongooseRegistry.get(unwrappedId) || {};
|
|
420
|
+
if (idMeta.includeId !== true && unwrappedIdMeta.includeId !== true && mongooseProp.includeId !== true) {
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const def = extractMongooseDef2(shape[key], visited);
|
|
425
|
+
if (def && typeof def === "object" && def.__isDiscriminatorUnion) {
|
|
426
|
+
const mongoose2 = getMongoose();
|
|
427
|
+
if (mongoose2) {
|
|
428
|
+
const baseSchema = new mongoose2.Schema(def.baseDef, {
|
|
429
|
+
discriminatorKey: def.discriminatorKey,
|
|
430
|
+
_id: false
|
|
431
|
+
});
|
|
432
|
+
const discriminators = {};
|
|
433
|
+
for (const [dKey, dDef] of Object.entries(def.discriminators)) {
|
|
434
|
+
discriminators[dKey] = new mongoose2.Schema(dDef, { _id: false });
|
|
714
435
|
}
|
|
715
|
-
|
|
436
|
+
objDef[key] = {
|
|
437
|
+
type: baseSchema,
|
|
438
|
+
discriminators
|
|
439
|
+
};
|
|
440
|
+
} else {
|
|
441
|
+
objDef[key] = def;
|
|
442
|
+
}
|
|
443
|
+
} else if (typeof def === "object" && def !== null && !Array.isArray(def)) {
|
|
444
|
+
const { includeId, ...cleanDef } = def;
|
|
445
|
+
objDef[key] = cleanDef;
|
|
446
|
+
} else {
|
|
447
|
+
objDef[key] = def;
|
|
716
448
|
}
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
449
|
+
callHookSync("schema:object:field", { key, schema: shape[key], objDef, visited });
|
|
450
|
+
}
|
|
451
|
+
let result;
|
|
452
|
+
const shouldBeSubSchema = isField && mongooseProp.schema !== false;
|
|
453
|
+
if (shouldBeSubSchema && !mongooseProp.type) {
|
|
454
|
+
const mongoose2 = getMongoose();
|
|
455
|
+
if (mongoose2) {
|
|
456
|
+
const options = typeof mongooseProp.schema === "object" ? mongooseProp.schema : {};
|
|
457
|
+
const { plugins, ...schemaOptions } = options;
|
|
458
|
+
const subSchema = new mongoose2.Schema(objDef, schemaOptions);
|
|
459
|
+
if (plugins && Array.isArray(plugins)) {
|
|
460
|
+
for (const plugin of plugins) {
|
|
461
|
+
subSchema.plugin(plugin);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
mongooseProp.type = subSchema;
|
|
721
465
|
}
|
|
722
|
-
|
|
723
|
-
|
|
466
|
+
}
|
|
467
|
+
if (mongooseProp.type) {
|
|
468
|
+
const mongoose2 = getMongoose();
|
|
469
|
+
const isSchema = mongoose2 && (mongooseProp.type instanceof mongoose2.Schema || ((_a = mongooseProp.type.constructor) == null ? void 0 : _a.name) === "Schema");
|
|
470
|
+
if (!isSchema) {
|
|
471
|
+
Object.assign(mongooseProp, objDef);
|
|
724
472
|
}
|
|
725
|
-
|
|
726
|
-
|
|
473
|
+
result = mongooseProp;
|
|
474
|
+
} else {
|
|
475
|
+
Object.assign(mongooseProp, objDef);
|
|
476
|
+
const topLevelOptions = /* @__PURE__ */ new Set([
|
|
477
|
+
"collection",
|
|
478
|
+
"versionKey",
|
|
479
|
+
"timestamps",
|
|
480
|
+
"discriminatorKey",
|
|
481
|
+
"strict",
|
|
482
|
+
"id",
|
|
483
|
+
"_id",
|
|
484
|
+
"minimize",
|
|
485
|
+
"validateBeforeSave",
|
|
486
|
+
"schema"
|
|
487
|
+
]);
|
|
488
|
+
const hasFieldMetadata = Object.keys(mongooseProp).some((k) => {
|
|
489
|
+
if (Object.prototype.hasOwnProperty.call(objDef, k))
|
|
490
|
+
return false;
|
|
491
|
+
if (topLevelOptions.has(k))
|
|
492
|
+
return false;
|
|
493
|
+
if (k === "required" && mongooseProp[k] === false)
|
|
494
|
+
return false;
|
|
495
|
+
return true;
|
|
496
|
+
});
|
|
497
|
+
result = hasFieldMetadata ? mongooseProp : objDef;
|
|
498
|
+
}
|
|
499
|
+
callHookSync("schema:object:after", { schema: unwrapped, mongooseProp, objDef, result });
|
|
500
|
+
return result;
|
|
501
|
+
}
|
|
502
|
+
function handleArray(unwrapped, mongooseProp, visited, extractMongooseDef2) {
|
|
503
|
+
var _a;
|
|
504
|
+
callHookSync("schema:array:before", { schema: unwrapped, mongooseProp, visited });
|
|
505
|
+
const element = unwrapped.element || unwrapped._def.valueType || unwrapped._def.rest || ((_a = unwrapped._def.items) == null ? void 0 : _a[0]);
|
|
506
|
+
const mongoose2 = getMongoose();
|
|
507
|
+
const innerDef = element ? extractMongooseDef2(element, visited) : (mongoose2 == null ? void 0 : mongoose2.Schema.Types.Mixed) || "Mixed";
|
|
508
|
+
if (!mongooseProp.type) {
|
|
509
|
+
if (innerDef && typeof innerDef === "object" && innerDef.__isDiscriminatorUnion && mongoose2) {
|
|
510
|
+
const discriminators = {};
|
|
511
|
+
for (const [dKey, dDef] of Object.entries(innerDef.discriminators)) {
|
|
512
|
+
discriminators[dKey] = new mongoose2.Schema(dDef, { _id: false });
|
|
513
|
+
}
|
|
514
|
+
mongooseProp.type = [
|
|
515
|
+
new mongoose2.Schema({}, {
|
|
516
|
+
discriminatorKey: innerDef.discriminatorKey,
|
|
517
|
+
_id: false
|
|
518
|
+
})
|
|
519
|
+
];
|
|
520
|
+
mongooseProp.discriminators = discriminators;
|
|
521
|
+
} else {
|
|
522
|
+
const innerType = innerDef.type || innerDef;
|
|
523
|
+
mongooseProp.type = [innerType];
|
|
524
|
+
if (typeof innerDef === "object") {
|
|
525
|
+
const { type: _extractedType, ...innerMeta } = innerDef;
|
|
526
|
+
Object.assign(mongooseProp, innerMeta);
|
|
527
|
+
mongooseProp.type = [innerType];
|
|
528
|
+
}
|
|
727
529
|
}
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
530
|
+
}
|
|
531
|
+
callHookSync("schema:array:after", { schema: unwrapped, mongooseProp, innerDef });
|
|
532
|
+
}
|
|
533
|
+
function handleRecord(unwrapped, mongooseProp, visited, extractMongooseDef2) {
|
|
534
|
+
var _a, _b;
|
|
535
|
+
callHookSync("schema:record:before", { schema: unwrapped, mongooseProp, visited });
|
|
536
|
+
const type = (_a = unwrapped._def) == null ? void 0 : _a.type;
|
|
537
|
+
const isMap = type === "map";
|
|
538
|
+
const valueType = unwrapped.valueType || unwrapped.valueSchema || unwrapped._def.valueType || unwrapped._def.valueSchema || unwrapped._def.innerType;
|
|
539
|
+
let innerDef;
|
|
540
|
+
if (!mongooseProp.type || mongooseProp.type === Map || mongooseProp.type === Object) {
|
|
541
|
+
mongooseProp.type = isMap ? Map : Object;
|
|
542
|
+
const finalValueType = valueType || unwrapped.valueSchema || ((_b = unwrapped._def) == null ? void 0 : _b.valueSchema);
|
|
543
|
+
if (finalValueType) {
|
|
544
|
+
innerDef = extractMongooseDef2(finalValueType, visited);
|
|
545
|
+
mongooseProp.of = innerDef.type || innerDef;
|
|
737
546
|
}
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
547
|
+
}
|
|
548
|
+
callHookSync("schema:record:after", { schema: unwrapped, mongooseProp, innerDef });
|
|
549
|
+
}
|
|
550
|
+
function extractMongooseDef(schema, visited = /* @__PURE__ */ new Map(), isField = false, noWrap = false) {
|
|
551
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
552
|
+
if (visited.size === 0) {
|
|
553
|
+
callHookSync("converter:before", { schema, visited });
|
|
554
|
+
}
|
|
555
|
+
callHookSync("converter:start", { schema, visited });
|
|
556
|
+
const { schema: unwrapped, features } = unwrapZodSchema(schema);
|
|
557
|
+
const meta = mongooseRegistry.get(schema) || {};
|
|
558
|
+
const mongooseProp = getMongooseMeta(schema);
|
|
559
|
+
callHookSync("converter:unwrapped", {
|
|
560
|
+
schema,
|
|
561
|
+
unwrapped,
|
|
562
|
+
features,
|
|
563
|
+
meta: mongooseProp,
|
|
564
|
+
mongooseProp
|
|
565
|
+
});
|
|
566
|
+
if (features.isOptional === true && mongooseProp.type && mongooseProp.required !== true) {
|
|
567
|
+
mongooseProp.required = false;
|
|
568
|
+
}
|
|
569
|
+
if (visited.has(schema)) {
|
|
570
|
+
const existing = visited.get(schema);
|
|
571
|
+
if (existing === mongooseProp) {
|
|
572
|
+
return existing;
|
|
756
573
|
}
|
|
757
|
-
if (
|
|
758
|
-
|
|
574
|
+
if (Object.keys(meta).length > 0) {
|
|
575
|
+
Object.assign(existing, mongooseProp);
|
|
759
576
|
}
|
|
760
|
-
|
|
761
|
-
|
|
577
|
+
return existing;
|
|
578
|
+
}
|
|
579
|
+
visited.set(schema, mongooseProp);
|
|
580
|
+
if (features.default !== void 0) {
|
|
581
|
+
mongooseProp.default = features.default;
|
|
582
|
+
}
|
|
583
|
+
if (features.required === false) {
|
|
584
|
+
mongooseProp.required = false;
|
|
585
|
+
}
|
|
586
|
+
if (features.readOnly === true) {
|
|
587
|
+
mongooseProp.readOnly = true;
|
|
588
|
+
}
|
|
589
|
+
mapZodChecksToMongoose(features.checks, mongooseProp);
|
|
590
|
+
const def = unwrapped._def;
|
|
591
|
+
if (!def) {
|
|
592
|
+
callHookSync("converter:after", {
|
|
593
|
+
schema,
|
|
594
|
+
mongooseProp
|
|
595
|
+
});
|
|
596
|
+
return mongooseProp;
|
|
597
|
+
}
|
|
598
|
+
const { type } = def;
|
|
599
|
+
callHookSync("converter:node", {
|
|
600
|
+
schema: unwrapped,
|
|
601
|
+
mongooseProp,
|
|
602
|
+
type
|
|
603
|
+
});
|
|
604
|
+
if (type === "object") {
|
|
605
|
+
const wrapperFn = (s, v) => extractMongooseDef(s, v, true);
|
|
606
|
+
const result = handleObject(unwrapped, mongooseProp, visited, wrapperFn, isField && !noWrap);
|
|
607
|
+
callHookSync("converter:after", {
|
|
608
|
+
schema,
|
|
609
|
+
mongooseProp: result
|
|
610
|
+
});
|
|
611
|
+
if (typeof result === "object" && result !== null && !Array.isArray(result)) {
|
|
612
|
+
delete result.includeId;
|
|
762
613
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
delete mongooseProp[key];
|
|
788
|
-
}
|
|
789
|
-
}
|
|
614
|
+
return result;
|
|
615
|
+
}
|
|
616
|
+
if (type === "array" || type === "set" || type === "tuple") {
|
|
617
|
+
handleArray(unwrapped, mongooseProp, visited, (s, v) => extractMongooseDef(s, v, true));
|
|
618
|
+
}
|
|
619
|
+
if (type === "record" || type === "map") {
|
|
620
|
+
handleRecord(unwrapped, mongooseProp, visited, (s, v) => extractMongooseDef(s, v, true));
|
|
621
|
+
}
|
|
622
|
+
if (type === "intersection") {
|
|
623
|
+
const left = extractMongooseDef(unwrapped._def.left, visited, isField, true);
|
|
624
|
+
const right = extractMongooseDef(unwrapped._def.right, visited, isField, true);
|
|
625
|
+
if (typeof left === "object" && typeof right === "object") {
|
|
626
|
+
Object.assign(mongooseProp, left, right);
|
|
627
|
+
if (isField && !noWrap && mongooseProp.schema !== false && !mongooseProp.type) {
|
|
628
|
+
const mongoose2 = getMongoose();
|
|
629
|
+
if (mongoose2) {
|
|
630
|
+
const options = typeof mongooseProp.schema === "object" ? mongooseProp.schema : {};
|
|
631
|
+
const { plugins, ...schemaOptions } = options;
|
|
632
|
+
const definition = { ...mongooseProp };
|
|
633
|
+
delete definition.schema;
|
|
634
|
+
const subSchema = new mongoose2.Schema(definition, schemaOptions);
|
|
635
|
+
if (plugins && Array.isArray(plugins)) {
|
|
636
|
+
for (const plugin of plugins) {
|
|
637
|
+
subSchema.plugin(plugin);
|
|
790
638
|
}
|
|
639
|
+
}
|
|
640
|
+
mongooseProp.type = subSchema;
|
|
641
|
+
for (const key of Object.keys(mongooseProp)) {
|
|
642
|
+
if (key !== "type")
|
|
643
|
+
delete mongooseProp[key];
|
|
644
|
+
}
|
|
791
645
|
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
646
|
+
}
|
|
647
|
+
} else if (!mongooseProp.type) {
|
|
648
|
+
mongooseProp.type = ((_a = getMongoose()) == null ? void 0 : _a.Schema.Types.Mixed) || "Mixed";
|
|
795
649
|
}
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
};
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
const { schema: unwrappedDisc } = unwrapZodSchema(discriminatorProp);
|
|
833
|
-
const discriminatorValue = unwrappedDisc._def.value ?? unwrappedDisc._def.values?.[0];
|
|
834
|
-
// Use a fresh Map for each option to avoid cross-contamination of visited nodes
|
|
835
|
-
const optionDef = extractMongooseDef(option, new Map(), true, true);
|
|
836
|
-
if (optionDef && typeof optionDef === 'object' && !Array.isArray(optionDef)) {
|
|
837
|
-
const cleanOptionDef = { ...optionDef };
|
|
838
|
-
delete cleanOptionDef[discriminatorKey];
|
|
839
|
-
discriminators[discriminatorValue] = cleanOptionDef;
|
|
840
|
-
allOptionDefs.push(cleanOptionDef);
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
// Identify common fields present in ALL options to move to baseDef
|
|
844
|
-
const baseDef = {};
|
|
845
|
-
if (allOptionDefs.length > 0) {
|
|
846
|
-
const firstOption = allOptionDefs[0];
|
|
847
|
-
for (const key of Object.keys(firstOption)) {
|
|
848
|
-
const isCommon = allOptionDefs.every((def) => {
|
|
849
|
-
if (!(key in def))
|
|
850
|
-
return false;
|
|
851
|
-
// Simple check for equality of definitions (can be improved)
|
|
852
|
-
return JSON.stringify(def[key]) === JSON.stringify(firstOption[key]);
|
|
853
|
-
});
|
|
854
|
-
if (isCommon) {
|
|
855
|
-
baseDef[key] = firstOption[key];
|
|
856
|
-
// Remove from discriminators to avoid duplication
|
|
857
|
-
for (const def of allOptionDefs) {
|
|
858
|
-
delete def[key];
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
const result = {
|
|
864
|
-
__isDiscriminatorUnion: true,
|
|
865
|
-
discriminatorKey,
|
|
866
|
-
discriminators,
|
|
867
|
-
baseDef,
|
|
868
|
-
validate: {
|
|
869
|
-
validator(_v) {
|
|
870
|
-
try {
|
|
871
|
-
// Ensure we include the discriminator key in the object being validated
|
|
872
|
-
const mongoose = getMongoose();
|
|
873
|
-
const doc = mongoose && this instanceof mongoose.Document ? this.toObject() : this || {};
|
|
874
|
-
schema.parse(doc);
|
|
875
|
-
return true;
|
|
876
|
-
}
|
|
877
|
-
catch (err) {
|
|
878
|
-
const message = err?.errors?.[0]?.message || err.message;
|
|
879
|
-
if (this && typeof this.invalidate === 'function') {
|
|
880
|
-
this.invalidate(discriminatorKey, `Zod validation failed: ${message}`);
|
|
881
|
-
}
|
|
882
|
-
return false;
|
|
883
|
-
}
|
|
884
|
-
},
|
|
885
|
-
message: (props) => `Validation failed for ${props.path}`,
|
|
886
|
-
},
|
|
887
|
-
};
|
|
888
|
-
// If this is wrapped in a meta object, ensure we return the result properly
|
|
889
|
-
if (mongooseProp && typeof mongooseProp === 'object' && !Array.isArray(mongooseProp)) {
|
|
890
|
-
Object.assign(mongooseProp, result);
|
|
891
|
-
callHookSync('schema:union:after', {
|
|
892
|
-
schema: unwrapped,
|
|
893
|
-
mongooseProp,
|
|
894
|
-
ctx: unionCtx,
|
|
895
|
-
});
|
|
896
|
-
return mongooseProp;
|
|
897
|
-
}
|
|
898
|
-
callHookSync('schema:union:after', {
|
|
899
|
-
schema: unwrapped,
|
|
900
|
-
mongooseProp: result,
|
|
901
|
-
ctx: unionCtx,
|
|
902
|
-
});
|
|
903
|
-
return result;
|
|
904
|
-
}
|
|
905
|
-
if (getMongoose()?.Schema.Types.Union &&
|
|
906
|
-
unionCtx.isSimpleUnion &&
|
|
907
|
-
options.length > 0 &&
|
|
908
|
-
!unionCtx.isXor) {
|
|
909
|
-
mongooseProp.type = mongoose.Schema.Types.Union;
|
|
910
|
-
mongooseProp.of = options.map((opt) => {
|
|
911
|
-
const def = extractMongooseDef(opt, visited, true, true);
|
|
912
|
-
return def.type || def;
|
|
913
|
-
});
|
|
650
|
+
}
|
|
651
|
+
if ((type === "union" || type === "discriminatedunion" || type === "discriminated_union" || type === "xor") && !mongooseProp.type) {
|
|
652
|
+
const mongoose2 = getMongoose();
|
|
653
|
+
const options = unwrapped.options || unwrapped._def.options;
|
|
654
|
+
const discriminatorKey = unwrapped._def.discriminator;
|
|
655
|
+
const unionCtx = {
|
|
656
|
+
isSimpleUnion: false,
|
|
657
|
+
isObjectUnion: false,
|
|
658
|
+
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)
|
|
659
|
+
};
|
|
660
|
+
if (Array.isArray(options) && options.length > 0) {
|
|
661
|
+
unionCtx.isSimpleUnion = options.every((opt) => {
|
|
662
|
+
const { type: type2 } = unwrapZodSchema(opt).schema._def;
|
|
663
|
+
return ["string", "number", "boolean", "date", "bigint", "literal"].includes(type2);
|
|
664
|
+
});
|
|
665
|
+
unionCtx.isObjectUnion = options.every((opt) => {
|
|
666
|
+
const { type: type2 } = unwrapZodSchema(opt).schema._def;
|
|
667
|
+
return type2 === "object";
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
callHookSync("schema:union:before", { schema: unwrapped, mongooseProp, ctx: unionCtx });
|
|
671
|
+
if (discriminatorKey && unionCtx.isObjectUnion) {
|
|
672
|
+
const discriminators = {};
|
|
673
|
+
const allOptionDefs = [];
|
|
674
|
+
for (const option of options) {
|
|
675
|
+
const { schema: unwrappedOpt } = unwrapZodSchema(option);
|
|
676
|
+
const { shape } = unwrappedOpt._def;
|
|
677
|
+
const discriminatorProp = shape[discriminatorKey];
|
|
678
|
+
const { schema: unwrappedDisc } = unwrapZodSchema(discriminatorProp);
|
|
679
|
+
const discriminatorValue = (_f = unwrappedDisc._def.value) != null ? _f : (_e = unwrappedDisc._def.values) == null ? void 0 : _e[0];
|
|
680
|
+
const optionDef = extractMongooseDef(option, /* @__PURE__ */ new Map(), true, true);
|
|
681
|
+
if (optionDef && typeof optionDef === "object" && !Array.isArray(optionDef)) {
|
|
682
|
+
const cleanOptionDef = { ...optionDef };
|
|
683
|
+
delete cleanOptionDef[discriminatorKey];
|
|
684
|
+
discriminators[discriminatorValue] = cleanOptionDef;
|
|
685
|
+
allOptionDefs.push(cleanOptionDef);
|
|
914
686
|
}
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
!Array.isArray(prop)) {
|
|
930
|
-
const existingType = mergedDef[key].type ||
|
|
931
|
-
mergedDef[key].instance ||
|
|
932
|
-
(typeof mergedDef[key] === 'function' ? mergedDef[key] : null);
|
|
933
|
-
const newType = prop.type ||
|
|
934
|
-
prop.instance ||
|
|
935
|
-
(typeof prop === 'function' ? prop : null);
|
|
936
|
-
const isMixed = (t) => !t ||
|
|
937
|
-
t === 'Mixed' ||
|
|
938
|
-
t === 'SchemaMixed' ||
|
|
939
|
-
t?.name === 'Mixed' ||
|
|
940
|
-
t?.instance === 'Mixed' ||
|
|
941
|
-
t?.name === 'SchemaMixed' ||
|
|
942
|
-
t?.instance === 'SchemaMixed' ||
|
|
943
|
-
(getMongoose()?.Schema.Types.Mixed &&
|
|
944
|
-
(t === getMongoose()?.Schema.Types.Mixed ||
|
|
945
|
-
t?.instance === 'Mixed' ||
|
|
946
|
-
t?.instance === 'SchemaMixed'));
|
|
947
|
-
if (isMixed(existingType) && !isMixed(newType)) {
|
|
948
|
-
mergedDef[key] = prop;
|
|
949
|
-
}
|
|
950
|
-
else if (!isMixed(existingType) && isMixed(newType)) ;
|
|
951
|
-
else {
|
|
952
|
-
Object.assign(mergedDef[key], prop);
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
else if (!mergedDef[key] ||
|
|
956
|
-
typeof mergedDef[key] !== 'object' ||
|
|
957
|
-
Array.isArray(mergedDef[key])) {
|
|
958
|
-
mergedDef[key] = prop;
|
|
959
|
-
}
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
if (isField && unionCtx.isXor) {
|
|
964
|
-
// For nested XOR, always use Mixed with validator to ensure mutual exclusivity
|
|
965
|
-
mongooseProp.type = mongoose?.Schema.Types.Mixed || 'Mixed';
|
|
966
|
-
mongooseProp.validate = {
|
|
967
|
-
validator(v) {
|
|
968
|
-
try {
|
|
969
|
-
schema.parse(v);
|
|
970
|
-
return true;
|
|
971
|
-
}
|
|
972
|
-
catch {
|
|
973
|
-
return false;
|
|
974
|
-
}
|
|
975
|
-
},
|
|
976
|
-
message: 'XOR validation failed',
|
|
977
|
-
};
|
|
978
|
-
}
|
|
979
|
-
else {
|
|
980
|
-
// For root or other object unions, merge properties
|
|
981
|
-
if (!mongooseProp.type ||
|
|
982
|
-
mongooseProp.type === (getMongoose()?.Schema.Types.Mixed || 'Mixed')) {
|
|
983
|
-
delete mongooseProp.type;
|
|
984
|
-
}
|
|
985
|
-
Object.assign(mongooseProp, mergedDef);
|
|
986
|
-
// If the object contains a 'type' property, Mongoose might misinterpret it as a field definition.
|
|
987
|
-
// We can hint that it's a nested object by using a Schema if 'type' is present along with other fields.
|
|
988
|
-
if (isField &&
|
|
989
|
-
Object.prototype.hasOwnProperty.call(mongooseProp, 'type') &&
|
|
990
|
-
Object.keys(mongooseProp).length > 1) {
|
|
991
|
-
const mongooseInstance = getMongoose();
|
|
992
|
-
if (mongooseInstance) {
|
|
993
|
-
mongooseProp.type = new mongooseInstance.Schema(mongooseProp, { _id: false });
|
|
994
|
-
for (const key of Object.keys(mongooseProp)) {
|
|
995
|
-
if (key !== 'type')
|
|
996
|
-
delete mongooseProp[key];
|
|
997
|
-
}
|
|
998
|
-
}
|
|
999
|
-
}
|
|
687
|
+
}
|
|
688
|
+
const baseDef = {};
|
|
689
|
+
if (allOptionDefs.length > 0) {
|
|
690
|
+
const firstOption = allOptionDefs[0];
|
|
691
|
+
for (const key of Object.keys(firstOption)) {
|
|
692
|
+
const isCommon = allOptionDefs.every((def2) => {
|
|
693
|
+
if (!(key in def2))
|
|
694
|
+
return false;
|
|
695
|
+
return JSON.stringify(def2[key]) === JSON.stringify(firstOption[key]);
|
|
696
|
+
});
|
|
697
|
+
if (isCommon) {
|
|
698
|
+
baseDef[key] = firstOption[key];
|
|
699
|
+
for (const def2 of allOptionDefs) {
|
|
700
|
+
delete def2[key];
|
|
1000
701
|
}
|
|
702
|
+
}
|
|
1001
703
|
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
}
|
|
1023
|
-
},
|
|
1024
|
-
message: (props) => {
|
|
1025
|
-
if (unionCtx.isXor)
|
|
1026
|
-
return 'XOR validation failed';
|
|
1027
|
-
try {
|
|
1028
|
-
schema.parse(props.value);
|
|
1029
|
-
}
|
|
1030
|
-
catch (err) {
|
|
1031
|
-
return `Union validation failed: ${err.message}`;
|
|
1032
|
-
}
|
|
1033
|
-
return 'Union validation failed';
|
|
1034
|
-
},
|
|
1035
|
-
};
|
|
704
|
+
}
|
|
705
|
+
const result = {
|
|
706
|
+
__isDiscriminatorUnion: true,
|
|
707
|
+
discriminatorKey,
|
|
708
|
+
discriminators,
|
|
709
|
+
baseDef,
|
|
710
|
+
validate: {
|
|
711
|
+
validator(_v) {
|
|
712
|
+
var _a2, _b2;
|
|
713
|
+
try {
|
|
714
|
+
const mongoose3 = getMongoose();
|
|
715
|
+
const doc = mongoose3 && this instanceof mongoose3.Document ? this.toObject() : this || {};
|
|
716
|
+
schema.parse(doc);
|
|
717
|
+
return true;
|
|
718
|
+
} catch (err) {
|
|
719
|
+
const message = ((_b2 = (_a2 = err == null ? void 0 : err.errors) == null ? void 0 : _a2[0]) == null ? void 0 : _b2.message) || err.message;
|
|
720
|
+
if (this && typeof this.invalidate === "function") {
|
|
721
|
+
this.invalidate(discriminatorKey, `Zod validation failed: ${message}`);
|
|
722
|
+
}
|
|
723
|
+
return false;
|
|
1036
724
|
}
|
|
725
|
+
},
|
|
726
|
+
message: (props) => `Validation failed for ${props.path}`
|
|
1037
727
|
}
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
728
|
+
};
|
|
729
|
+
if (mongooseProp && typeof mongooseProp === "object" && !Array.isArray(mongooseProp)) {
|
|
730
|
+
Object.assign(mongooseProp, result);
|
|
731
|
+
callHookSync("schema:union:after", {
|
|
732
|
+
schema: unwrapped,
|
|
733
|
+
mongooseProp,
|
|
734
|
+
ctx: unionCtx
|
|
1042
735
|
});
|
|
736
|
+
return mongooseProp;
|
|
737
|
+
}
|
|
738
|
+
callHookSync("schema:union:after", {
|
|
739
|
+
schema: unwrapped,
|
|
740
|
+
mongooseProp: result,
|
|
741
|
+
ctx: unionCtx
|
|
742
|
+
});
|
|
743
|
+
return result;
|
|
1043
744
|
}
|
|
1044
|
-
if (
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
if (type === 'bigint') {
|
|
1059
|
-
mongooseProp.type = typeof BigInt === 'undefined' ? Number : BigInt;
|
|
1060
|
-
}
|
|
1061
|
-
else {
|
|
1062
|
-
const typeMap = {
|
|
1063
|
-
string: String,
|
|
1064
|
-
number: Number,
|
|
1065
|
-
boolean: Boolean,
|
|
1066
|
-
date: Date,
|
|
1067
|
-
stringbool: Boolean,
|
|
1068
|
-
boolstring: Boolean,
|
|
1069
|
-
booleanstring: Boolean,
|
|
1070
|
-
};
|
|
1071
|
-
mongooseProp.type = typeMap[type];
|
|
1072
|
-
// Clever inference: If a transform occurred (which we know if the Zod type
|
|
1073
|
-
// is different from the default value type), prefer the default's type.
|
|
1074
|
-
if (mongooseProp.default !== undefined) {
|
|
1075
|
-
const defaultType = typeof mongooseProp.default;
|
|
1076
|
-
if (defaultType === 'boolean' && type !== 'boolean') {
|
|
1077
|
-
mongooseProp.type = Boolean;
|
|
1078
|
-
}
|
|
1079
|
-
else if (defaultType === 'number' && type !== 'number') {
|
|
1080
|
-
mongooseProp.type = Number;
|
|
1081
|
-
}
|
|
1082
|
-
else if (defaultType === 'string' && type !== 'string') {
|
|
1083
|
-
mongooseProp.type = String;
|
|
1084
|
-
}
|
|
1085
|
-
else if (mongooseProp.default instanceof Date && type !== 'date') {
|
|
1086
|
-
mongooseProp.type = Date;
|
|
1087
|
-
}
|
|
1088
|
-
}
|
|
1089
|
-
// Even cleverer: Check transformations for clues (e.g., stringbool, boolstring)
|
|
1090
|
-
if (mongooseProp.type === String && features.transformations) {
|
|
1091
|
-
for (const tx of features.transformations) {
|
|
1092
|
-
const txStr = tx.transform?.toString() || tx.toString();
|
|
1093
|
-
if (txStr.includes('stringbool') || txStr.includes('boolstring') || txStr.includes('booleanstring')) {
|
|
1094
|
-
mongooseProp.type = Boolean;
|
|
1095
|
-
break;
|
|
1096
|
-
}
|
|
1097
|
-
if (txStr.includes('=== "true"') || txStr.includes('=== \'true\'')) {
|
|
1098
|
-
mongooseProp.type = Boolean;
|
|
1099
|
-
break;
|
|
1100
|
-
}
|
|
1101
|
-
}
|
|
1102
|
-
}
|
|
1103
|
-
}
|
|
745
|
+
if (((_g = getMongoose()) == null ? void 0 : _g.Schema.Types.Union) && unionCtx.isSimpleUnion && options.length > 0 && !unionCtx.isXor) {
|
|
746
|
+
mongooseProp.type = mongoose2.Schema.Types.Union;
|
|
747
|
+
mongooseProp.of = options.map((opt) => {
|
|
748
|
+
const def2 = extractMongooseDef(opt, visited, true, true);
|
|
749
|
+
return def2.type || def2;
|
|
750
|
+
});
|
|
751
|
+
} else if (unionCtx.isObjectUnion && options.length > 0) {
|
|
752
|
+
const mergedDef = {};
|
|
753
|
+
for (const opt of options) {
|
|
754
|
+
const def2 = extractMongooseDef(opt, /* @__PURE__ */ new Map(), true, true);
|
|
755
|
+
if (typeof def2 === "object" && def2 !== null) {
|
|
756
|
+
for (const [key, prop] of Object.entries(def2)) {
|
|
757
|
+
if (typeof prop === "object" && prop !== null && !Array.isArray(prop)) {
|
|
758
|
+
prop.required = false;
|
|
1104
759
|
}
|
|
1105
|
-
if (
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
if (
|
|
1119
|
-
|
|
1120
|
-
|
|
760
|
+
if (mergedDef[key] && typeof mergedDef[key] === "object" && typeof prop === "object" && !Array.isArray(mergedDef[key]) && !Array.isArray(prop)) {
|
|
761
|
+
const existingType = mergedDef[key].type || mergedDef[key].instance || (typeof mergedDef[key] === "function" ? mergedDef[key] : null);
|
|
762
|
+
const newType = prop.type || prop.instance || (typeof prop === "function" ? prop : null);
|
|
763
|
+
const isMixed = (t) => {
|
|
764
|
+
var _a2, _b2;
|
|
765
|
+
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");
|
|
766
|
+
};
|
|
767
|
+
if (isMixed(existingType) && !isMixed(newType)) {
|
|
768
|
+
mergedDef[key] = prop;
|
|
769
|
+
} else if (!isMixed(existingType) && isMixed(newType)) ;
|
|
770
|
+
else {
|
|
771
|
+
Object.assign(mergedDef[key], prop);
|
|
772
|
+
}
|
|
773
|
+
} else if (!mergedDef[key] || typeof mergedDef[key] !== "object" || Array.isArray(mergedDef[key])) {
|
|
774
|
+
mergedDef[key] = prop;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
1121
777
|
}
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
778
|
+
}
|
|
779
|
+
if (isField && unionCtx.isXor) {
|
|
780
|
+
mongooseProp.type = (mongoose2 == null ? void 0 : mongoose2.Schema.Types.Mixed) || "Mixed";
|
|
781
|
+
mongooseProp.validate = {
|
|
782
|
+
validator(v) {
|
|
783
|
+
try {
|
|
784
|
+
schema.parse(v);
|
|
785
|
+
return true;
|
|
786
|
+
} catch {
|
|
787
|
+
return false;
|
|
788
|
+
}
|
|
789
|
+
},
|
|
790
|
+
message: "XOR validation failed"
|
|
791
|
+
};
|
|
792
|
+
} else {
|
|
793
|
+
if (!mongooseProp.type || mongooseProp.type === (((_h = getMongoose()) == null ? void 0 : _h.Schema.Types.Mixed) || "Mixed")) {
|
|
794
|
+
delete mongooseProp.type;
|
|
1131
795
|
}
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
796
|
+
Object.assign(mongooseProp, mergedDef);
|
|
797
|
+
if (isField && Object.prototype.hasOwnProperty.call(mongooseProp, "type") && Object.keys(mongooseProp).length > 1) {
|
|
798
|
+
const mongooseInstance3 = getMongoose();
|
|
799
|
+
if (mongooseInstance3) {
|
|
800
|
+
mongooseProp.type = new mongooseInstance3.Schema(mongooseProp, { _id: false });
|
|
801
|
+
for (const key of Object.keys(mongooseProp)) {
|
|
802
|
+
if (key !== "type")
|
|
803
|
+
delete mongooseProp[key];
|
|
804
|
+
}
|
|
805
|
+
}
|
|
1135
806
|
}
|
|
807
|
+
}
|
|
808
|
+
} else {
|
|
809
|
+
mongooseProp.type = (mongoose2 == null ? void 0 : mongoose2.Schema.Types.Mixed) || "Mixed";
|
|
810
|
+
if (isField && (type === "xor" || type === "discriminated_union" || type === "discriminatedunion" || type === "union") && !mongooseProp.ref && // Skip Zod validation for populated fields
|
|
811
|
+
!Array.isArray(mongooseProp.type) && // Skip Zod validation for arrays
|
|
812
|
+
mongooseProp.type !== Map && // Skip Zod validation for maps
|
|
813
|
+
!mongooseProp.of) {
|
|
814
|
+
mongooseProp.validate = {
|
|
815
|
+
validator(v) {
|
|
816
|
+
try {
|
|
817
|
+
schema.parse(v);
|
|
818
|
+
return true;
|
|
819
|
+
} catch (err) {
|
|
820
|
+
return false;
|
|
821
|
+
}
|
|
822
|
+
},
|
|
823
|
+
message: (props) => {
|
|
824
|
+
if (unionCtx.isXor)
|
|
825
|
+
return "XOR validation failed";
|
|
826
|
+
try {
|
|
827
|
+
schema.parse(props.value);
|
|
828
|
+
} catch (err) {
|
|
829
|
+
return `Union validation failed: ${err.message}`;
|
|
830
|
+
}
|
|
831
|
+
return "Union validation failed";
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
}
|
|
1136
835
|
}
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
836
|
+
callHookSync("schema:union:after", {
|
|
837
|
+
schema: unwrapped,
|
|
838
|
+
mongooseProp,
|
|
839
|
+
ctx: unionCtx
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
if (type === "literal" && !mongooseProp.type) {
|
|
843
|
+
mongooseProp.type = ((_i = getMongoose()) == null ? void 0 : _i.Schema.Types.Mixed) || "Mixed";
|
|
844
|
+
}
|
|
845
|
+
switch (type) {
|
|
846
|
+
case "string":
|
|
847
|
+
case "number":
|
|
848
|
+
case "boolean":
|
|
849
|
+
case "date":
|
|
850
|
+
case "bigint":
|
|
851
|
+
case "stringbool":
|
|
852
|
+
case "boolstring":
|
|
853
|
+
case "booleanstring": {
|
|
854
|
+
if (!mongooseProp.type) {
|
|
855
|
+
if (type === "bigint") {
|
|
856
|
+
mongooseProp.type = typeof BigInt === "undefined" ? Number : BigInt;
|
|
857
|
+
} else {
|
|
858
|
+
const typeMap = {
|
|
859
|
+
string: String,
|
|
860
|
+
number: Number,
|
|
861
|
+
boolean: Boolean,
|
|
862
|
+
date: Date,
|
|
863
|
+
stringbool: Boolean,
|
|
864
|
+
boolstring: Boolean,
|
|
865
|
+
booleanstring: Boolean
|
|
866
|
+
};
|
|
867
|
+
mongooseProp.type = typeMap[type];
|
|
868
|
+
if (mongooseProp.default !== void 0) {
|
|
869
|
+
const defaultType = typeof mongooseProp.default;
|
|
870
|
+
if (defaultType === "boolean" && type !== "boolean") {
|
|
871
|
+
mongooseProp.type = Boolean;
|
|
872
|
+
} else if (defaultType === "number" && type !== "number") {
|
|
873
|
+
mongooseProp.type = Number;
|
|
874
|
+
} else if (defaultType === "string" && type !== "string") {
|
|
875
|
+
mongooseProp.type = String;
|
|
876
|
+
} else if (mongooseProp.default instanceof Date && type !== "date") {
|
|
877
|
+
mongooseProp.type = Date;
|
|
1144
878
|
}
|
|
1145
|
-
|
|
1146
|
-
|
|
879
|
+
}
|
|
880
|
+
if (mongooseProp.type === String && features.transformations) {
|
|
881
|
+
for (const tx of features.transformations) {
|
|
882
|
+
const txStr = ((_j = tx.transform) == null ? void 0 : _j.toString()) || tx.toString();
|
|
883
|
+
if (txStr.includes("stringbool") || txStr.includes("boolstring") || txStr.includes("booleanstring")) {
|
|
884
|
+
mongooseProp.type = Boolean;
|
|
885
|
+
break;
|
|
886
|
+
}
|
|
887
|
+
if (txStr.includes('=== "true"') || txStr.includes("=== 'true'")) {
|
|
888
|
+
mongooseProp.type = Boolean;
|
|
889
|
+
break;
|
|
890
|
+
}
|
|
1147
891
|
}
|
|
1148
|
-
|
|
892
|
+
}
|
|
1149
893
|
}
|
|
1150
|
-
|
|
894
|
+
}
|
|
895
|
+
if (mongooseProp.required !== false)
|
|
896
|
+
mongooseProp.required = true;
|
|
897
|
+
break;
|
|
1151
898
|
}
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
899
|
+
case "enum":
|
|
900
|
+
case "nativeenum":
|
|
901
|
+
case "native_enum": {
|
|
902
|
+
if (!mongooseProp.type)
|
|
903
|
+
mongooseProp.type = String;
|
|
904
|
+
mongooseProp.enum = type === "enum" ? unwrapped.options || def.values : Object.values(unwrapped.enum || def.values);
|
|
905
|
+
if (mongooseProp.required !== false)
|
|
906
|
+
mongooseProp.required = true;
|
|
907
|
+
break;
|
|
1155
908
|
}
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
if (
|
|
1161
|
-
|
|
909
|
+
}
|
|
910
|
+
const mongooseInstance2 = getMongoose();
|
|
911
|
+
if (type === "any" || type === "unknown" || type === "custom") {
|
|
912
|
+
const cls = def.cls || unwrapped.cls;
|
|
913
|
+
if (cls === Buffer || typeof Uint8Array !== "undefined" && cls === Uint8Array) {
|
|
914
|
+
if (!mongooseProp.type)
|
|
915
|
+
mongooseProp.type = (mongooseInstance2 == null ? void 0 : mongooseInstance2.Schema.Types.Buffer) || "Buffer";
|
|
916
|
+
} else if (((cls == null ? void 0 : cls.name) === "ObjectId" || mongooseInstance2 && cls === mongooseInstance2.Types.ObjectId) && !mongooseProp.type) {
|
|
917
|
+
mongooseProp.type = (mongooseInstance2 == null ? void 0 : mongooseInstance2.Schema.Types.ObjectId) || "ObjectId";
|
|
1162
918
|
}
|
|
1163
|
-
|
|
919
|
+
}
|
|
920
|
+
if (type === "lazy") {
|
|
921
|
+
const inner = def.getter();
|
|
922
|
+
const result = extractMongooseDef(inner, visited, isField);
|
|
923
|
+
if (Object.keys(meta).length > 0 && result !== mongooseProp) {
|
|
924
|
+
if (typeof result === "object" && !Array.isArray(result)) {
|
|
925
|
+
Object.assign(mongooseProp, result);
|
|
926
|
+
} else {
|
|
927
|
+
mongooseProp.type = result.type || result;
|
|
928
|
+
}
|
|
929
|
+
return mongooseProp;
|
|
930
|
+
}
|
|
931
|
+
return result;
|
|
932
|
+
}
|
|
933
|
+
if (!mongooseProp.type && type !== "object") {
|
|
934
|
+
mongooseProp.type = ((_k = getMongoose()) == null ? void 0 : _k.Schema.Types.Mixed) || "Mixed";
|
|
935
|
+
}
|
|
936
|
+
callHookSync("converter:after", {
|
|
937
|
+
schema,
|
|
938
|
+
mongooseProp
|
|
939
|
+
});
|
|
940
|
+
if (typeof mongooseProp === "object" && mongooseProp !== null && !Array.isArray(mongooseProp)) {
|
|
941
|
+
delete mongooseProp.includeId;
|
|
942
|
+
}
|
|
943
|
+
return mongooseProp;
|
|
1164
944
|
}
|
|
1165
|
-
|
|
1166
|
-
/**
|
|
1167
|
-
* Converts a Zod schema to a Mongoose Schema instance.
|
|
1168
|
-
*/
|
|
1169
945
|
function toMongooseSchema(schema, options) {
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
946
|
+
var _a, _b;
|
|
947
|
+
const { schema: unwrapped } = unwrapZodSchema(schema);
|
|
948
|
+
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)) || {};
|
|
949
|
+
const { plugins, ...schemaOptions } = options || {};
|
|
950
|
+
const mergedOptions = {
|
|
951
|
+
// Also merge other schema options from meta if they exist
|
|
952
|
+
...meta.collection ? { collection: meta.collection } : {},
|
|
953
|
+
// eslint-disable-next-line unicorn/no-negated-condition
|
|
954
|
+
...meta.strict !== void 0 ? { strict: meta.strict } : {},
|
|
955
|
+
// eslint-disable-next-line unicorn/no-negated-condition
|
|
956
|
+
...meta.minimize !== void 0 ? { minimize: meta.minimize } : {},
|
|
957
|
+
// eslint-disable-next-line unicorn/no-negated-condition
|
|
958
|
+
...meta.validateBeforeSave !== void 0 ? { validateBeforeSave: meta.validateBeforeSave } : {},
|
|
959
|
+
// eslint-disable-next-line unicorn/no-negated-condition
|
|
960
|
+
...meta.versionKey !== void 0 ? { versionKey: meta.versionKey } : {},
|
|
961
|
+
...meta.id === void 0 ? {} : { id: meta.id },
|
|
962
|
+
...meta._id === void 0 ? {} : { _id: meta._id },
|
|
963
|
+
...meta.timestamps ? { timestamps: meta.timestamps } : {},
|
|
964
|
+
...meta.discriminatorKey ? { discriminatorKey: meta.discriminatorKey } : {},
|
|
965
|
+
...schemaOptions
|
|
966
|
+
};
|
|
967
|
+
let definition = extractMongooseDef(schema, /* @__PURE__ */ new Map(), false);
|
|
968
|
+
const mongoose2 = getMongoose();
|
|
969
|
+
if (!mongoose2) {
|
|
970
|
+
throw new Error("Mongoose must be installed to use toMongooseSchema. If you are in an ESM environment, ensure mongoose is loaded.");
|
|
971
|
+
}
|
|
972
|
+
let mongooseSchema;
|
|
973
|
+
if (definition && typeof definition === "object" && definition.__isDiscriminatorUnion) {
|
|
974
|
+
const { discriminatorKey, discriminators, baseDef, validate } = definition;
|
|
975
|
+
mongooseSchema = new mongoose2.Schema(baseDef, {
|
|
976
|
+
...mergedOptions,
|
|
977
|
+
discriminatorKey
|
|
978
|
+
});
|
|
979
|
+
if (validate) {
|
|
980
|
+
if (!mongooseSchema.path(discriminatorKey)) {
|
|
981
|
+
mongooseSchema.add({ [discriminatorKey]: { type: String } });
|
|
982
|
+
}
|
|
983
|
+
mongooseSchema.path(discriminatorKey).validate(validate);
|
|
1198
984
|
}
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
discriminatorKey,
|
|
1205
|
-
});
|
|
1206
|
-
if (validate) {
|
|
1207
|
-
if (!mongooseSchema.path(discriminatorKey)) {
|
|
1208
|
-
mongooseSchema.add({ [discriminatorKey]: { type: String } });
|
|
1209
|
-
}
|
|
1210
|
-
mongooseSchema.path(discriminatorKey).validate(validate);
|
|
1211
|
-
}
|
|
1212
|
-
for (const [key, dDef] of Object.entries(discriminators)) {
|
|
1213
|
-
if (mongooseSchema.discriminators && mongooseSchema.discriminators[key]) {
|
|
1214
|
-
continue;
|
|
1215
|
-
}
|
|
1216
|
-
mongooseSchema.discriminator(key, new mongoose.Schema(dDef, { _id: false }));
|
|
1217
|
-
}
|
|
985
|
+
for (const [key, dDef] of Object.entries(discriminators)) {
|
|
986
|
+
if (mongooseSchema.discriminators && mongooseSchema.discriminators[key]) {
|
|
987
|
+
continue;
|
|
988
|
+
}
|
|
989
|
+
mongooseSchema.discriminator(key, new mongoose2.Schema(dDef, { _id: false }));
|
|
1218
990
|
}
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
for (const value of Object.values(definition)) {
|
|
1227
|
-
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
1228
|
-
delete value.includeId;
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
991
|
+
} else {
|
|
992
|
+
if (typeof definition === "object" && definition !== null) {
|
|
993
|
+
const { includeId, ...cleanDefinition } = definition;
|
|
994
|
+
definition = cleanDefinition;
|
|
995
|
+
for (const value of Object.values(definition)) {
|
|
996
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
997
|
+
delete value.includeId;
|
|
1231
998
|
}
|
|
1232
|
-
|
|
999
|
+
}
|
|
1233
1000
|
}
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1001
|
+
mongooseSchema = new mongoose2.Schema(definition, mergedOptions);
|
|
1002
|
+
}
|
|
1003
|
+
if (plugins && Array.isArray(plugins)) {
|
|
1004
|
+
for (const plugin of plugins) {
|
|
1005
|
+
mongooseSchema.plugin(plugin);
|
|
1239
1006
|
}
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1007
|
+
}
|
|
1008
|
+
callHookSync("schema:created", {
|
|
1009
|
+
schema,
|
|
1010
|
+
mongooseSchema,
|
|
1011
|
+
options: mergedOptions
|
|
1012
|
+
});
|
|
1013
|
+
return mongooseSchema;
|
|
1247
1014
|
}
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
const
|
|
1253
|
-
const
|
|
1254
|
-
const
|
|
1255
|
-
|
|
1256
|
-
|
|
1015
|
+
function populateZodSchema(schema, keys) {
|
|
1016
|
+
const { shape } = schema;
|
|
1017
|
+
const newShape = { ...shape };
|
|
1018
|
+
const keysToPopulate = keys || Object.keys(shape);
|
|
1019
|
+
const populateField = (field) => {
|
|
1020
|
+
const meta = getMongooseMeta(field);
|
|
1021
|
+
const { schema: unwrapped, features } = unwrapZodSchema(field);
|
|
1022
|
+
let result = field;
|
|
1023
|
+
if (meta == null ? void 0 : meta.refSchema) {
|
|
1024
|
+
result = meta.refSchema;
|
|
1025
|
+
} else if (unwrapped instanceof z.ZodArray) {
|
|
1026
|
+
const populatedInner = populateField(unwrapped.element);
|
|
1027
|
+
if (populatedInner !== unwrapped.element) {
|
|
1028
|
+
result = z.array(populatedInner);
|
|
1029
|
+
}
|
|
1030
|
+
} else if (unwrapped instanceof z.ZodObject) {
|
|
1031
|
+
result = populateZodSchema(unwrapped);
|
|
1257
1032
|
}
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1033
|
+
if (result !== field && result !== unwrapped) {
|
|
1034
|
+
if (features.isOptional && !(result instanceof z.ZodOptional))
|
|
1035
|
+
result = result.optional();
|
|
1036
|
+
if (features.isNullable && !(result instanceof z.ZodNullable))
|
|
1037
|
+
result = result.nullable();
|
|
1038
|
+
if (features.default !== void 0 && !(result instanceof z.ZodDefault))
|
|
1039
|
+
result = result.default(features.default);
|
|
1040
|
+
}
|
|
1041
|
+
return result;
|
|
1042
|
+
};
|
|
1043
|
+
for (const key of keysToPopulate) {
|
|
1044
|
+
newShape[key] = populateField(shape[key]);
|
|
1045
|
+
}
|
|
1046
|
+
return z.object(newShape);
|
|
1047
|
+
}
|
|
1048
|
+
const genTimestampsSchema = (createdAtField = "createdAt", updatedAtField = "updatedAt") => {
|
|
1049
|
+
if (createdAtField != null && updatedAtField != null && createdAtField === updatedAtField) {
|
|
1050
|
+
throw new Error("`createdAt` and `updatedAt` fields must be different");
|
|
1051
|
+
}
|
|
1052
|
+
const shape = {};
|
|
1053
|
+
if (createdAtField != null)
|
|
1054
|
+
shape[createdAtField] = z.date().default(() => /* @__PURE__ */ new Date());
|
|
1055
|
+
if (updatedAtField != null)
|
|
1056
|
+
shape[updatedAtField] = z.date().default(() => /* @__PURE__ */ new Date());
|
|
1057
|
+
return shape;
|
|
1058
|
+
};
|
|
1059
|
+
const bufferMongooseGetter = (value) => value != null && value._bsontype === "Binary" ? value.buffer : value;
|
|
1060
|
+
const preprocessFn = (val) => val === null ? void 0 : val;
|
|
1061
|
+
const zObjectId = (options) => {
|
|
1062
|
+
const mongooseInstance2 = getMongoose();
|
|
1063
|
+
const objectIdSchema = z.custom((val) => mongooseInstance2 && val instanceof mongooseInstance2.Types.ObjectId);
|
|
1064
|
+
const baseUnion = z.preprocess(preprocessFn, z.union([objectIdSchema, z.string().regex(/^[\dA-Fa-f]{24}$/, "Invalid ObjectId")]));
|
|
1065
|
+
const inputSchema = z.codec(baseUnion, objectIdSchema, {
|
|
1066
|
+
decode: (val) => {
|
|
1067
|
+
if (!mongooseInstance2)
|
|
1068
|
+
return val;
|
|
1069
|
+
if (val instanceof mongooseInstance2.Types.ObjectId) {
|
|
1070
|
+
return val;
|
|
1071
|
+
}
|
|
1072
|
+
return new mongooseInstance2.Types.ObjectId(val);
|
|
1073
|
+
},
|
|
1074
|
+
encode: (val) => val.toString()
|
|
1075
|
+
});
|
|
1076
|
+
return withMongoose(inputSchema, {
|
|
1077
|
+
type: (mongooseInstance2 == null ? void 0 : mongooseInstance2.Schema.Types.ObjectId) || "ObjectId",
|
|
1078
|
+
...options
|
|
1079
|
+
});
|
|
1261
1080
|
};
|
|
1262
1081
|
const zBuffer = (options) => {
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
}
|
|
1266
|
-
const mongooseInstance = getMongoose();
|
|
1267
|
-
return withMongoose(v4.z.custom((val) => (mongooseInstance && val instanceof Buffer) || val instanceof Uint8Array), { type: mongooseInstance?.Schema.Types.Buffer || 'Buffer', ...options });
|
|
1082
|
+
const mongooseInstance2 = getMongoose();
|
|
1083
|
+
return withMongoose(z.custom((val) => mongooseInstance2 && val instanceof Buffer || val instanceof Uint8Array), { type: (mongooseInstance2 == null ? void 0 : mongooseInstance2.Schema.Types.Buffer) || "Buffer", ...options });
|
|
1268
1084
|
};
|
|
1269
|
-
// ============================================================================
|
|
1270
|
-
// 3. REF SCHEMAS & DYNAMIC POPULATION
|
|
1271
|
-
// ============================================================================
|
|
1272
1085
|
const zRef = (ref, schema, options) => {
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
});
|
|
1286
|
-
return result;
|
|
1287
|
-
};
|
|
1288
|
-
function populateZodSchema(schema, keys) {
|
|
1289
|
-
const { shape } = schema;
|
|
1290
|
-
const newShape = { ...shape };
|
|
1291
|
-
const keysToPopulate = keys || Object.keys(shape);
|
|
1292
|
-
const populateField = (field) => {
|
|
1293
|
-
const meta = getMongooseMeta(field);
|
|
1294
|
-
const { schema: unwrapped, features } = unwrapZodSchema(field);
|
|
1295
|
-
let result = field;
|
|
1296
|
-
if (meta?.refSchema) {
|
|
1297
|
-
result = meta.refSchema;
|
|
1298
|
-
}
|
|
1299
|
-
else if (unwrapped instanceof v4.z.ZodArray) {
|
|
1300
|
-
const populatedInner = populateField(unwrapped.element);
|
|
1301
|
-
if (populatedInner !== unwrapped.element) {
|
|
1302
|
-
result = v4.z.array(populatedInner);
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
else if (unwrapped instanceof v4.z.ZodObject) {
|
|
1306
|
-
result = populateZodSchema(unwrapped);
|
|
1307
|
-
}
|
|
1308
|
-
if (result !== field && result !== unwrapped) {
|
|
1309
|
-
if (features.isOptional && !(result instanceof v4.z.ZodOptional))
|
|
1310
|
-
result = result.optional();
|
|
1311
|
-
if (features.isNullable && !(result instanceof v4.z.ZodNullable))
|
|
1312
|
-
result = result.nullable();
|
|
1313
|
-
if (features.default !== undefined && !(result instanceof v4.z.ZodDefault))
|
|
1314
|
-
result = result.default(features.default);
|
|
1315
|
-
}
|
|
1316
|
-
return result;
|
|
1317
|
-
};
|
|
1318
|
-
for (const key of keysToPopulate) {
|
|
1319
|
-
newShape[key] = populateField(shape[key]);
|
|
1320
|
-
}
|
|
1321
|
-
return v4.z.object(newShape);
|
|
1322
|
-
}
|
|
1323
|
-
// ============================================================================
|
|
1324
|
-
// 4. TIMESTAMPS & OUTPUT FORMATTING
|
|
1325
|
-
// ============================================================================
|
|
1326
|
-
const DateFieldZod = () => v4.z.date().default(() => new Date());
|
|
1327
|
-
const genTimestampsSchema = (createdAtField = 'createdAt', updatedAtField = 'updatedAt') => {
|
|
1328
|
-
if (createdAtField != null &&
|
|
1329
|
-
updatedAtField != null &&
|
|
1330
|
-
createdAtField === updatedAtField) {
|
|
1331
|
-
throw new Error('`createdAt` and `updatedAt` fields must be different');
|
|
1332
|
-
}
|
|
1333
|
-
const shape = {};
|
|
1334
|
-
if (createdAtField != null)
|
|
1335
|
-
shape[createdAtField] = withMongoose(DateFieldZod(), { immutable: true, index: true });
|
|
1336
|
-
if (updatedAtField != null)
|
|
1337
|
-
shape[updatedAtField] = withMongoose(DateFieldZod(), { index: true });
|
|
1338
|
-
return shape;
|
|
1086
|
+
const mongooseInstance2 = getMongoose();
|
|
1087
|
+
const objectIdSchema = zObjectId();
|
|
1088
|
+
const base = z.codec(z.union([objectIdSchema, schema]), objectIdSchema, {
|
|
1089
|
+
decode: (val) => typeof val === "object" && val !== null && "_id" in val ? val._id : val,
|
|
1090
|
+
encode: (val) => val
|
|
1091
|
+
});
|
|
1092
|
+
return withMongoose(base, {
|
|
1093
|
+
type: (mongooseInstance2 == null ? void 0 : mongooseInstance2.Schema.Types.ObjectId) || "ObjectId",
|
|
1094
|
+
ref,
|
|
1095
|
+
refSchema: schema,
|
|
1096
|
+
...options
|
|
1097
|
+
});
|
|
1339
1098
|
};
|
|
1340
|
-
const bufferMongooseGetter = (value) => value != null && value._bsontype === 'Binary' ? value.buffer : value;
|
|
1341
|
-
|
|
1342
|
-
// ============================================================================
|
|
1343
|
-
// 6. INITIALIZATION RUNTIME COMPONENT
|
|
1344
|
-
// ============================================================================
|
|
1345
|
-
/**
|
|
1346
|
-
* Converts a standard Mongoose model into a `StrictModel` with advanced type-safe population.
|
|
1347
|
-
*
|
|
1348
|
-
* @template UserInferredType The Zod-inferred type of the document (e.g. `z.infer<typeof Schema>`).
|
|
1349
|
-
* @param name The model name to register or retrieve from Mongoose.
|
|
1350
|
-
* @param mongooseSchema The Mongoose schema instance.
|
|
1351
|
-
* @returns A `StrictModel` instance with enhanced type safety for population.
|
|
1352
|
-
*
|
|
1353
|
-
* @example
|
|
1354
|
-
* ```typescript
|
|
1355
|
-
* const PostModel = toStrictModel<Post>('Post', postSchema);
|
|
1356
|
-
* const post = await PostModel.findOne().populate('author').exec();
|
|
1357
|
-
* // post.author is now fully typed
|
|
1358
|
-
* ```
|
|
1359
|
-
*/
|
|
1360
1099
|
function toStrictModel(name, mongooseSchema) {
|
|
1361
|
-
|
|
1362
|
-
|
|
1100
|
+
const rawModel = mongoose__default.model(name, mongooseSchema);
|
|
1101
|
+
return rawModel;
|
|
1363
1102
|
}
|
|
1364
1103
|
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
bufferMongooseGetter: bufferMongooseGetter_1,
|
|
1387
|
-
callHookSync: callHookSync_1,
|
|
1388
|
-
default: dist,
|
|
1389
|
-
extractMongooseDef: extractMongooseDef_1,
|
|
1390
|
-
genTimestampsSchema: genTimestampsSchema_1,
|
|
1391
|
-
getFrontendMode: getFrontendMode_1,
|
|
1392
|
-
getMongoose: getMongoose_1,
|
|
1393
|
-
getMongooseMeta: getMongooseMeta_1,
|
|
1394
|
-
hooks: hooks_1,
|
|
1395
|
-
mongooseRegistry: mongooseRegistry_1,
|
|
1396
|
-
populateZodSchema: populateZodSchema_1,
|
|
1397
|
-
setFrontendMode: setFrontendMode_1,
|
|
1398
|
-
setMongoose: setMongoose_1,
|
|
1399
|
-
toMongooseSchema: toMongooseSchema_1,
|
|
1400
|
-
toStrictModel: toStrictModel_1,
|
|
1401
|
-
withMongoose: withMongoose_1,
|
|
1402
|
-
zBuffer: zBuffer_1,
|
|
1403
|
-
zObjectId: zObjectId_1,
|
|
1404
|
-
zRef: zRef_1
|
|
1405
|
-
}, [dist]);
|
|
1104
|
+
const mongooseZod = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
|
|
1105
|
+
__proto__: null,
|
|
1106
|
+
bufferMongooseGetter: bufferMongooseGetter,
|
|
1107
|
+
callHookSync: callHookSync,
|
|
1108
|
+
extractMongooseDef: extractMongooseDef,
|
|
1109
|
+
genTimestampsSchema: genTimestampsSchema,
|
|
1110
|
+
getFrontendMode: getFrontendMode,
|
|
1111
|
+
getMongoose: getMongoose,
|
|
1112
|
+
getMongooseMeta: getMongooseMeta,
|
|
1113
|
+
hooks: hooks,
|
|
1114
|
+
mongooseRegistry: mongooseRegistry,
|
|
1115
|
+
populateZodSchema: populateZodSchema,
|
|
1116
|
+
setFrontendMode: setFrontendMode,
|
|
1117
|
+
setMongoose: setMongoose,
|
|
1118
|
+
toMongooseSchema: toMongooseSchema,
|
|
1119
|
+
toStrictModel: toStrictModel,
|
|
1120
|
+
withMongoose: withMongoose,
|
|
1121
|
+
zBuffer: zBuffer,
|
|
1122
|
+
zObjectId: zObjectId,
|
|
1123
|
+
zRef: zRef
|
|
1124
|
+
}, Symbol.toStringTag, { value: 'Module' }));
|
|
1406
1125
|
|
|
1407
|
-
|
|
1126
|
+
setMongoose(mongoose.default || mongoose);
|
|
1408
1127
|
let _filename = "/sandbox/main.ts";
|
|
1409
1128
|
let _dirname = "/sandbox";
|
|
1410
1129
|
try {
|
|
@@ -1447,7 +1166,7 @@ const parse_post = defineEventHandler(async (event) => {
|
|
|
1447
1166
|
const sandboxRequire = (moduleName) => {
|
|
1448
1167
|
if (moduleName === "zod" || moduleName === "zod/v4") return zod;
|
|
1449
1168
|
if (moduleName === "@nullix/zod-mongoose") return mongooseZod;
|
|
1450
|
-
if (moduleName === "mongoose") return mongoose
|
|
1169
|
+
if (moduleName === "mongoose") return mongoose.default || mongoose;
|
|
1451
1170
|
throw new Error(`Security Exception: Module "${moduleName}" is blocked.`);
|
|
1452
1171
|
};
|
|
1453
1172
|
const sandbox = {
|
|
@@ -1478,8 +1197,8 @@ const parse_post = defineEventHandler(async (event) => {
|
|
|
1478
1197
|
if (!schema || !schema._def) {
|
|
1479
1198
|
throw new Error("You must `export default` a valid Zod schema.");
|
|
1480
1199
|
}
|
|
1481
|
-
const extractedDef =
|
|
1482
|
-
const mongooseSchema =
|
|
1200
|
+
const extractedDef = extractMongooseDef(schema);
|
|
1201
|
+
const mongooseSchema = toMongooseSchema(schema);
|
|
1483
1202
|
const formatOpts = { depth: null, showHidden: false, colors: false };
|
|
1484
1203
|
const formatValue = (val) => {
|
|
1485
1204
|
if (typeof val === "function") {
|