@zenstackhq/zod 3.5.6 → 3.6.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/dist/chunk-CfYAbeIz.mjs +13 -0
- package/dist/index.cjs +668 -807
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +185 -147
- package/dist/index.d.mts +265 -0
- package/dist/index.mjs +680 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +10 -9
- package/dist/index.d.ts +0 -229
- package/dist/index.js +0 -826
- package/dist/index.js.map +0 -1
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,680 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
|
|
2
|
+
import { ExpressionUtils, SchemaAccessor } from "@zenstackhq/schema";
|
|
3
|
+
import Decimal from "decimal.js";
|
|
4
|
+
import z$1, { z } from "zod";
|
|
5
|
+
//#region src/error.ts
|
|
6
|
+
/**
|
|
7
|
+
* Error representing failures in Zod schema building.
|
|
8
|
+
*/
|
|
9
|
+
var SchemaFactoryError = class extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/utils.ts
|
|
16
|
+
var utils_exports = /* @__PURE__ */ __exportAll({
|
|
17
|
+
addBigIntValidation: () => addBigIntValidation,
|
|
18
|
+
addCustomValidation: () => addCustomValidation,
|
|
19
|
+
addDecimalValidation: () => addDecimalValidation,
|
|
20
|
+
addListValidation: () => addListValidation,
|
|
21
|
+
addNumberValidation: () => addNumberValidation,
|
|
22
|
+
addStringValidation: () => addStringValidation,
|
|
23
|
+
collectFieldRefs: () => collectFieldRefs
|
|
24
|
+
});
|
|
25
|
+
function getArgValue(expr) {
|
|
26
|
+
if (!expr || !ExpressionUtils.isLiteral(expr)) return;
|
|
27
|
+
return expr.value;
|
|
28
|
+
}
|
|
29
|
+
function addStringValidation(schema, attributes) {
|
|
30
|
+
if (!attributes || attributes.length === 0) return schema;
|
|
31
|
+
let result = schema;
|
|
32
|
+
for (const attr of attributes) switch (attr.name) {
|
|
33
|
+
case "@length": {
|
|
34
|
+
const min = getArgValue(attr.args?.find((a) => a.name === "min")?.value);
|
|
35
|
+
if (min !== void 0) result = result.min(min);
|
|
36
|
+
const max = getArgValue(attr.args?.find((a) => a.name === "max")?.value);
|
|
37
|
+
if (max !== void 0) result = result.max(max);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case "@startsWith": {
|
|
41
|
+
const value = getArgValue(attr.args?.[0]?.value);
|
|
42
|
+
if (value !== void 0) result = result.startsWith(value);
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case "@endsWith": {
|
|
46
|
+
const value = getArgValue(attr.args?.[0]?.value);
|
|
47
|
+
if (value !== void 0) result = result.endsWith(value);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case "@contains": {
|
|
51
|
+
const value = getArgValue(attr.args?.[0]?.value);
|
|
52
|
+
if (value !== void 0) result = result.includes(value);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case "@regex": {
|
|
56
|
+
const pattern = getArgValue(attr.args?.[0]?.value);
|
|
57
|
+
if (pattern !== void 0) result = result.regex(new RegExp(pattern));
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case "@email":
|
|
61
|
+
result = result.email();
|
|
62
|
+
break;
|
|
63
|
+
case "@datetime":
|
|
64
|
+
result = result.datetime();
|
|
65
|
+
break;
|
|
66
|
+
case "@url":
|
|
67
|
+
result = result.url();
|
|
68
|
+
break;
|
|
69
|
+
case "@trim":
|
|
70
|
+
result = result.trim();
|
|
71
|
+
break;
|
|
72
|
+
case "@lower":
|
|
73
|
+
result = result.toLowerCase();
|
|
74
|
+
break;
|
|
75
|
+
case "@upper":
|
|
76
|
+
result = result.toUpperCase();
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
function addNumberValidation(schema, attributes) {
|
|
82
|
+
if (!attributes || attributes.length === 0) return schema;
|
|
83
|
+
let result = schema;
|
|
84
|
+
for (const attr of attributes) {
|
|
85
|
+
const val = getArgValue(attr.args?.[0]?.value);
|
|
86
|
+
if (val === void 0) continue;
|
|
87
|
+
switch (attr.name) {
|
|
88
|
+
case "@gt":
|
|
89
|
+
result = result.gt(val);
|
|
90
|
+
break;
|
|
91
|
+
case "@gte":
|
|
92
|
+
result = result.gte(val);
|
|
93
|
+
break;
|
|
94
|
+
case "@lt":
|
|
95
|
+
result = result.lt(val);
|
|
96
|
+
break;
|
|
97
|
+
case "@lte":
|
|
98
|
+
result = result.lte(val);
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
function addBigIntValidation(schema, attributes) {
|
|
105
|
+
if (!attributes || attributes.length === 0) return schema;
|
|
106
|
+
let result = schema;
|
|
107
|
+
for (const attr of attributes) {
|
|
108
|
+
const val = getArgValue(attr.args?.[0]?.value);
|
|
109
|
+
if (val === void 0) continue;
|
|
110
|
+
switch (attr.name) {
|
|
111
|
+
case "@gt":
|
|
112
|
+
result = result.gt(BigInt(val));
|
|
113
|
+
break;
|
|
114
|
+
case "@gte":
|
|
115
|
+
result = result.gte(BigInt(val));
|
|
116
|
+
break;
|
|
117
|
+
case "@lt":
|
|
118
|
+
result = result.lt(BigInt(val));
|
|
119
|
+
break;
|
|
120
|
+
case "@lte":
|
|
121
|
+
result = result.lte(BigInt(val));
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
function addDecimalValidation(schema, attributes, addExtraValidation) {
|
|
128
|
+
let result = schema;
|
|
129
|
+
if (schema instanceof z.ZodString) result = schema.superRefine((v, ctx) => {
|
|
130
|
+
try {
|
|
131
|
+
new Decimal(v);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
ctx.addIssue({
|
|
134
|
+
code: "custom",
|
|
135
|
+
message: `Invalid decimal: ${err}`
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}).transform((val) => new Decimal(val));
|
|
139
|
+
function refine(schema, op, value) {
|
|
140
|
+
return schema.superRefine((v, ctx) => {
|
|
141
|
+
const { error } = z.number()[op](value).safeParse(v.toNumber());
|
|
142
|
+
error?.issues.forEach((issue) => {
|
|
143
|
+
if (op === "gt" || op === "gte") ctx.addIssue({
|
|
144
|
+
code: "too_small",
|
|
145
|
+
origin: "number",
|
|
146
|
+
minimum: value,
|
|
147
|
+
type: "decimal",
|
|
148
|
+
inclusive: op === "gte",
|
|
149
|
+
message: issue.message
|
|
150
|
+
});
|
|
151
|
+
else ctx.addIssue({
|
|
152
|
+
code: "too_big",
|
|
153
|
+
origin: "number",
|
|
154
|
+
maximum: value,
|
|
155
|
+
type: "decimal",
|
|
156
|
+
inclusive: op === "lte",
|
|
157
|
+
message: issue.message
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
if (attributes && addExtraValidation) for (const attr of attributes) {
|
|
163
|
+
const val = getArgValue(attr.args?.[0]?.value);
|
|
164
|
+
if (val === void 0) continue;
|
|
165
|
+
switch (attr.name) {
|
|
166
|
+
case "@gt":
|
|
167
|
+
result = refine(result, "gt", val);
|
|
168
|
+
break;
|
|
169
|
+
case "@gte":
|
|
170
|
+
result = refine(result, "gte", val);
|
|
171
|
+
break;
|
|
172
|
+
case "@lt":
|
|
173
|
+
result = refine(result, "lt", val);
|
|
174
|
+
break;
|
|
175
|
+
case "@lte":
|
|
176
|
+
result = refine(result, "lte", val);
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
function addListValidation(schema, attributes) {
|
|
183
|
+
if (!attributes || attributes.length === 0) return schema;
|
|
184
|
+
let result = schema;
|
|
185
|
+
for (const attr of attributes) if (attr.name === "@length") {
|
|
186
|
+
const min = getArgValue(attr.args?.find((a) => a.name === "min")?.value);
|
|
187
|
+
if (min !== void 0) result = result.min(min);
|
|
188
|
+
const max = getArgValue(attr.args?.find((a) => a.name === "max")?.value);
|
|
189
|
+
if (max !== void 0) result = result.max(max);
|
|
190
|
+
}
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Recursively collects all field names referenced by `kind: 'field'` nodes
|
|
195
|
+
* inside an expression tree.
|
|
196
|
+
*/
|
|
197
|
+
function collectFieldRefs(expr) {
|
|
198
|
+
const refs = /* @__PURE__ */ new Set();
|
|
199
|
+
function walk(e) {
|
|
200
|
+
switch (e.kind) {
|
|
201
|
+
case "field":
|
|
202
|
+
refs.add(e.field);
|
|
203
|
+
break;
|
|
204
|
+
case "unary":
|
|
205
|
+
walk(e.operand);
|
|
206
|
+
break;
|
|
207
|
+
case "binary":
|
|
208
|
+
walk(e.left);
|
|
209
|
+
walk(e.right);
|
|
210
|
+
break;
|
|
211
|
+
case "call":
|
|
212
|
+
e.args?.forEach(walk);
|
|
213
|
+
break;
|
|
214
|
+
case "array":
|
|
215
|
+
e.items.forEach(walk);
|
|
216
|
+
break;
|
|
217
|
+
case "member":
|
|
218
|
+
walk(e.receiver);
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
walk(expr);
|
|
223
|
+
return refs;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Applies `@@validate` rules from `attributes` to `schema` as Zod refinements.
|
|
227
|
+
*
|
|
228
|
+
* When `presentFields` is provided, only rules whose every field reference is
|
|
229
|
+
* present in the set are applied. Rules that reference a field absent from the
|
|
230
|
+
* set are silently skipped — they cannot be evaluated correctly against a
|
|
231
|
+
* partial payload (e.g. when `select` or `omit` has been used).
|
|
232
|
+
*
|
|
233
|
+
* Omit `presentFields` (or pass `undefined`) to apply all rules
|
|
234
|
+
* unconditionally, which is the correct behaviour for full-model schemas.
|
|
235
|
+
*
|
|
236
|
+
* Note: `@@validate` conditions are restricted by the ZModel compiler to
|
|
237
|
+
* scalar fields of the same model only — relation fields are a compile error.
|
|
238
|
+
* A flat field-name set is therefore sufficient.
|
|
239
|
+
*/
|
|
240
|
+
function addCustomValidation(schema, attributes, presentFields) {
|
|
241
|
+
const attrs = attributes?.filter((a) => a.name === "@@validate");
|
|
242
|
+
if (!attrs || attrs.length === 0) return schema;
|
|
243
|
+
let result = schema;
|
|
244
|
+
for (const attr of attrs) {
|
|
245
|
+
const expr = attr.args?.[0]?.value;
|
|
246
|
+
if (!expr) continue;
|
|
247
|
+
if (presentFields !== void 0) {
|
|
248
|
+
if ([...collectFieldRefs(expr)].some((ref) => !presentFields.has(ref))) continue;
|
|
249
|
+
}
|
|
250
|
+
const message = getArgValue(attr.args?.[1]?.value);
|
|
251
|
+
const pathExpr = attr.args?.[2]?.value;
|
|
252
|
+
let path = void 0;
|
|
253
|
+
if (pathExpr && ExpressionUtils.isArray(pathExpr)) path = pathExpr.items.map((e) => ExpressionUtils.getLiteralValue(e));
|
|
254
|
+
result = applyValidation(result, expr, message, path);
|
|
255
|
+
}
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
function applyValidation(schema, expr, message, path) {
|
|
259
|
+
const options = {};
|
|
260
|
+
if (message) options.error = message;
|
|
261
|
+
if (path) options.path = path;
|
|
262
|
+
return schema.refine((data) => Boolean(evalExpression(data, expr)), options);
|
|
263
|
+
}
|
|
264
|
+
function evalExpression(data, expr) {
|
|
265
|
+
switch (expr.kind) {
|
|
266
|
+
case "literal": return expr.value;
|
|
267
|
+
case "array": return expr.items.map((item) => evalExpression(data, item));
|
|
268
|
+
case "field": return evalField(data, expr);
|
|
269
|
+
case "member": return evalMember(data, expr);
|
|
270
|
+
case "unary": return evalUnary(data, expr);
|
|
271
|
+
case "binary": return evalBinary(data, expr);
|
|
272
|
+
case "call": return evalCall(data, expr);
|
|
273
|
+
case "this": return data ?? null;
|
|
274
|
+
case "null": return null;
|
|
275
|
+
case "binding": throw new SchemaFactoryError("Binding expression is not supported in validation expressions");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Sentinel value returned by `evalField` when a field key is entirely absent
|
|
280
|
+
* from the data object (as opposed to being present with a `null` value).
|
|
281
|
+
* Used by comparison operators to skip @@validate rules against missing
|
|
282
|
+
* optional fields (e.g. when `optionality: 'all'` produces a partial payload).
|
|
283
|
+
*/
|
|
284
|
+
const ABSENT = Symbol("absent");
|
|
285
|
+
function evalField(data, e) {
|
|
286
|
+
if (data == null || !(e.field in data)) return ABSENT;
|
|
287
|
+
return data[e.field] ?? null;
|
|
288
|
+
}
|
|
289
|
+
function evalUnary(data, expr) {
|
|
290
|
+
const operand = evalExpression(data, expr.operand);
|
|
291
|
+
switch (expr.op) {
|
|
292
|
+
case "!": return !operand;
|
|
293
|
+
default: throw new SchemaFactoryError(`Unsupported unary operator: ${expr.op}`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function evalBinary(data, expr) {
|
|
297
|
+
const left = evalExpression(data, expr.left);
|
|
298
|
+
const right = evalExpression(data, expr.right);
|
|
299
|
+
switch (expr.op) {
|
|
300
|
+
case "&&": return Boolean(left) && Boolean(right);
|
|
301
|
+
case "||": return Boolean(left) || Boolean(right);
|
|
302
|
+
case "==": return (left === ABSENT ? null : left) == (right === ABSENT ? null : right);
|
|
303
|
+
case "!=": return (left === ABSENT ? null : left) != (right === ABSENT ? null : right);
|
|
304
|
+
case "<":
|
|
305
|
+
case "<=":
|
|
306
|
+
case ">":
|
|
307
|
+
case ">=":
|
|
308
|
+
if (left === ABSENT || right === ABSENT) return true;
|
|
309
|
+
return expr.op === "<" ? left < right : expr.op === "<=" ? left <= right : expr.op === ">" ? left > right : left >= right;
|
|
310
|
+
case "?":
|
|
311
|
+
if (!Array.isArray(left)) return false;
|
|
312
|
+
return left.some((item) => item === right);
|
|
313
|
+
case "!":
|
|
314
|
+
if (!Array.isArray(left)) return false;
|
|
315
|
+
return left.every((item) => item === right);
|
|
316
|
+
case "^":
|
|
317
|
+
if (!Array.isArray(left)) return false;
|
|
318
|
+
return !left.some((item) => item === right);
|
|
319
|
+
case "in":
|
|
320
|
+
if (!Array.isArray(right)) return false;
|
|
321
|
+
return right.includes(left);
|
|
322
|
+
default: throw new SchemaFactoryError(`Unsupported binary operator: ${expr.op}`);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
function evalMember(data, expr) {
|
|
326
|
+
let result = evalExpression(data, expr.receiver);
|
|
327
|
+
for (const member of expr.members) {
|
|
328
|
+
if (!result || typeof result !== "object") return;
|
|
329
|
+
result = result[member];
|
|
330
|
+
}
|
|
331
|
+
return result ?? null;
|
|
332
|
+
}
|
|
333
|
+
function evalCall(data, expr) {
|
|
334
|
+
const f = expr.function;
|
|
335
|
+
const fieldArg = expr.args?.[0] ? evalExpression(data, expr.args[0]) : void 0;
|
|
336
|
+
switch (f) {
|
|
337
|
+
case "length":
|
|
338
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
339
|
+
invariant(typeof fieldArg === "string" || Array.isArray(fieldArg), `"${f}" first argument must be a string or a list`);
|
|
340
|
+
return fieldArg.length;
|
|
341
|
+
case "startsWith":
|
|
342
|
+
case "endsWith":
|
|
343
|
+
case "contains": {
|
|
344
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
345
|
+
invariant(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
346
|
+
invariant(expr.args?.[1], `"${f}" requires a search argument`);
|
|
347
|
+
const search = getArgValue(expr.args?.[1]);
|
|
348
|
+
const caseInsensitive = getArgValue(expr.args?.[2]) ?? false;
|
|
349
|
+
const applyStringOp = (x, y) => {
|
|
350
|
+
switch (f) {
|
|
351
|
+
case "startsWith": return x.startsWith(y);
|
|
352
|
+
case "endsWith": return x.endsWith(y);
|
|
353
|
+
case "contains": return x.includes(y);
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
return caseInsensitive ? applyStringOp(fieldArg.toLowerCase(), search.toLowerCase()) : applyStringOp(fieldArg, search);
|
|
357
|
+
}
|
|
358
|
+
case "regex": {
|
|
359
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
360
|
+
invariant(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
361
|
+
const pattern = getArgValue(expr.args?.[1]);
|
|
362
|
+
invariant(pattern !== void 0, `"${f}" requires a pattern argument`);
|
|
363
|
+
return new RegExp(pattern).test(fieldArg);
|
|
364
|
+
}
|
|
365
|
+
case "isEmail":
|
|
366
|
+
case "isUrl":
|
|
367
|
+
case "isDateTime": {
|
|
368
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
369
|
+
invariant(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
370
|
+
const fn = f === "isEmail" ? "email" : f === "isUrl" ? "url" : "datetime";
|
|
371
|
+
return z.string()[fn]().safeParse(fieldArg).success;
|
|
372
|
+
}
|
|
373
|
+
case "has":
|
|
374
|
+
case "hasEvery":
|
|
375
|
+
case "hasSome": {
|
|
376
|
+
invariant(expr.args?.[1], `${f} requires a search argument`);
|
|
377
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
378
|
+
invariant(Array.isArray(fieldArg), `"${f}" first argument must be an array field`);
|
|
379
|
+
const search = evalExpression(data, expr.args?.[1]);
|
|
380
|
+
if (f === "has") return fieldArg.some((item) => item === search);
|
|
381
|
+
else if (f === "hasEvery") {
|
|
382
|
+
invariant(Array.isArray(search), "hasEvery second argument must be an array");
|
|
383
|
+
return search.every((v) => fieldArg.some((item) => item === v));
|
|
384
|
+
} else {
|
|
385
|
+
invariant(Array.isArray(search), "hasSome second argument must be an array");
|
|
386
|
+
return search.some((v) => fieldArg.some((item) => item === v));
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
case "isEmpty":
|
|
390
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
391
|
+
invariant(Array.isArray(fieldArg), `"${f}" first argument must be an array field`);
|
|
392
|
+
return fieldArg.length === 0;
|
|
393
|
+
default: throw new SchemaFactoryError(`Unsupported function "${f}"`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function invariant(condition, message) {
|
|
397
|
+
if (!condition) throw new SchemaFactoryError(message ?? "Invariant failed");
|
|
398
|
+
}
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/factory.ts
|
|
401
|
+
function createSchemaFactory(schema) {
|
|
402
|
+
return new SchemaFactory(schema);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Recursive Zod schema that validates a `RawOptions` object at runtime,
|
|
406
|
+
* enforcing the same mutual-exclusion rules that the TypeScript union type
|
|
407
|
+
* enforces at compile time:
|
|
408
|
+
* - `select` and `include` cannot be used together.
|
|
409
|
+
* - `select` and `omit` cannot be used together.
|
|
410
|
+
* Nested relation options are validated with the same rules.
|
|
411
|
+
*/
|
|
412
|
+
const rawOptionsSchema = z$1.lazy(() => z$1.strictObject({
|
|
413
|
+
select: z$1.record(z$1.string(), z$1.union([z$1.literal(true), rawOptionsSchema])).optional(),
|
|
414
|
+
include: z$1.record(z$1.string(), z$1.union([z$1.literal(true), rawOptionsSchema])).optional(),
|
|
415
|
+
omit: z$1.record(z$1.string(), z$1.literal(true)).optional(),
|
|
416
|
+
optionality: z$1.enum(["all", "defaults"]).optional()
|
|
417
|
+
}).superRefine((val, ctx) => {
|
|
418
|
+
if (val.select && val.include) ctx.addIssue({
|
|
419
|
+
code: "custom",
|
|
420
|
+
message: "`select` and `include` cannot be used together"
|
|
421
|
+
});
|
|
422
|
+
if (val.select && val.omit) ctx.addIssue({
|
|
423
|
+
code: "custom",
|
|
424
|
+
message: "`select` and `omit` cannot be used together"
|
|
425
|
+
});
|
|
426
|
+
}));
|
|
427
|
+
var SchemaFactory = class {
|
|
428
|
+
schema;
|
|
429
|
+
constructor(_schema) {
|
|
430
|
+
this.schema = new SchemaAccessor(_schema);
|
|
431
|
+
}
|
|
432
|
+
makeModelSchema(model, options) {
|
|
433
|
+
const modelDef = this.schema.requireModel(model);
|
|
434
|
+
if (!options) {
|
|
435
|
+
const fields = {};
|
|
436
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
437
|
+
if (fieldDef.relation) continue;
|
|
438
|
+
fields[fieldName] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
439
|
+
}
|
|
440
|
+
const shape = z$1.strictObject(fields);
|
|
441
|
+
return this.applyDescription(addCustomValidation(shape, modelDef.attributes), modelDef.attributes);
|
|
442
|
+
}
|
|
443
|
+
const rawOptions = rawOptionsSchema.parse(options);
|
|
444
|
+
const fields = this.buildFieldsWithOptions(model, rawOptions);
|
|
445
|
+
const optionalizedFields = this.applyOptionality(fields, model, rawOptions.optionality);
|
|
446
|
+
const shape = z$1.strictObject(optionalizedFields);
|
|
447
|
+
const presentFields = this.buildPresentFields(model, rawOptions);
|
|
448
|
+
const withValidation = addCustomValidation(shape, modelDef.attributes, presentFields);
|
|
449
|
+
return this.applyDescription(withValidation, modelDef.attributes);
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* @deprecated Use `makeModelSchema(model, { optionality: 'defaults' })` instead.
|
|
453
|
+
*/
|
|
454
|
+
makeModelCreateSchema(model) {
|
|
455
|
+
const modelDef = this.schema.requireModel(model);
|
|
456
|
+
const fields = {};
|
|
457
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
458
|
+
if (fieldDef.relation || fieldDef.computed || fieldDef.isDiscriminator) continue;
|
|
459
|
+
let fieldSchema = this.makeScalarFieldSchema(fieldDef);
|
|
460
|
+
if (fieldDef.optional || fieldDef.default !== void 0 || fieldDef.updatedAt) fieldSchema = fieldSchema.optional();
|
|
461
|
+
fields[fieldName] = this.applyDescription(fieldSchema, fieldDef.attributes);
|
|
462
|
+
}
|
|
463
|
+
const shape = z$1.strictObject(fields);
|
|
464
|
+
return this.applyDescription(addCustomValidation(shape, modelDef.attributes), modelDef.attributes);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* @deprecated Use `makeModelSchema(model, { optionality: 'all' })` instead.
|
|
468
|
+
*/
|
|
469
|
+
makeModelUpdateSchema(model) {
|
|
470
|
+
const modelDef = this.schema.requireModel(model);
|
|
471
|
+
const fields = {};
|
|
472
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
473
|
+
if (fieldDef.relation || fieldDef.computed || fieldDef.isDiscriminator) continue;
|
|
474
|
+
let fieldSchema = this.makeScalarFieldSchema(fieldDef);
|
|
475
|
+
fieldSchema = fieldSchema.optional();
|
|
476
|
+
fields[fieldName] = this.applyDescription(fieldSchema, fieldDef.attributes);
|
|
477
|
+
}
|
|
478
|
+
const shape = z$1.strictObject(fields);
|
|
479
|
+
return this.applyDescription(addCustomValidation(shape, modelDef.attributes), modelDef.attributes);
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Applies the `optionality` option to a fields map.
|
|
483
|
+
*
|
|
484
|
+
* - `"all"` — wraps every field in `z.ZodOptional`.
|
|
485
|
+
* - `"defaults"` — only wraps fields that have a `@default` attribute or
|
|
486
|
+
* are `@updatedAt` in `z.ZodOptional`. Fields that are
|
|
487
|
+
* already optional (nullable optional) retain their shape;
|
|
488
|
+
* we just add the outer optional layer.
|
|
489
|
+
* - `undefined` — returns the fields map unchanged.
|
|
490
|
+
*/
|
|
491
|
+
applyOptionality(fields, model, optionality) {
|
|
492
|
+
if (!optionality) return fields;
|
|
493
|
+
const modelDef = this.schema.requireModel(model);
|
|
494
|
+
const result = {};
|
|
495
|
+
for (const [fieldName, fieldSchema] of Object.entries(fields)) if (optionality === "all") result[fieldName] = this.wrapOptionalPreservingMeta(fieldSchema);
|
|
496
|
+
else {
|
|
497
|
+
const fieldDef = modelDef.fields[fieldName];
|
|
498
|
+
result[fieldName] = fieldDef && (fieldDef.default !== void 0 || fieldDef.updatedAt || fieldDef.optional) ? this.wrapOptionalPreservingMeta(fieldSchema) : fieldSchema;
|
|
499
|
+
}
|
|
500
|
+
return result;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Internal loose options shape used at runtime (we've already validated the
|
|
504
|
+
* type-level constraints via the overload signatures).
|
|
505
|
+
*/
|
|
506
|
+
buildFieldsWithOptions(model, options) {
|
|
507
|
+
const { select, include, omit } = options;
|
|
508
|
+
const modelDef = this.schema.requireModel(model);
|
|
509
|
+
const fields = {};
|
|
510
|
+
if (select) for (const [key, value] of Object.entries(select)) {
|
|
511
|
+
const fieldDef = modelDef.fields[key];
|
|
512
|
+
if (!fieldDef) throw new SchemaFactoryError(`Field "${key}" does not exist on model "${model}"`);
|
|
513
|
+
if (fieldDef.relation) {
|
|
514
|
+
const subOptions = typeof value === "object" ? value : void 0;
|
|
515
|
+
const relSchema = this.makeRelationFieldSchema(fieldDef, subOptions);
|
|
516
|
+
fields[key] = this.applyDescription(this.applyCardinality(relSchema, fieldDef).optional(), fieldDef.attributes);
|
|
517
|
+
} else {
|
|
518
|
+
if (typeof value === "object") throw new SchemaFactoryError(`Field "${key}" on model "${model}" is a scalar field and cannot have nested options`);
|
|
519
|
+
fields[key] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
if (omit) for (const key of Object.keys(omit)) {
|
|
524
|
+
const fieldDef = modelDef.fields[key];
|
|
525
|
+
if (!fieldDef) throw new SchemaFactoryError(`Field "${key}" does not exist on model "${model}"`);
|
|
526
|
+
if (fieldDef.relation) throw new SchemaFactoryError(`Field "${key}" on model "${model}" is a relation field and cannot be used in "omit"`);
|
|
527
|
+
}
|
|
528
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
529
|
+
if (fieldDef.relation) continue;
|
|
530
|
+
if (omit?.[fieldName] === true) continue;
|
|
531
|
+
fields[fieldName] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
532
|
+
}
|
|
533
|
+
if (include) for (const [key, value] of Object.entries(include)) {
|
|
534
|
+
const fieldDef = modelDef.fields[key];
|
|
535
|
+
if (!fieldDef) throw new SchemaFactoryError(`Field "${key}" does not exist on model "${model}"`);
|
|
536
|
+
if (!fieldDef.relation) throw new SchemaFactoryError(`Field "${key}" on model "${model}" is not a relation field and cannot be used in "include"`);
|
|
537
|
+
const subOptions = typeof value === "object" ? value : void 0;
|
|
538
|
+
const relSchema = this.makeRelationFieldSchema(fieldDef, subOptions);
|
|
539
|
+
fields[key] = this.applyDescription(this.applyCardinality(relSchema, fieldDef).optional(), fieldDef.attributes);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
return fields;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Returns the set of scalar field names that will be present in the
|
|
546
|
+
* resulting schema after applying `options`. Used by `addCustomValidation`
|
|
547
|
+
* to skip `@@validate` rules that reference an absent field.
|
|
548
|
+
*
|
|
549
|
+
* Only scalar fields matter here because `@@validate` conditions are
|
|
550
|
+
* restricted by the ZModel compiler to scalar fields of the same model.
|
|
551
|
+
*/
|
|
552
|
+
buildPresentFields(model, options) {
|
|
553
|
+
const { select, omit } = options;
|
|
554
|
+
const modelDef = this.schema.requireModel(model);
|
|
555
|
+
const fields = /* @__PURE__ */ new Set();
|
|
556
|
+
if (select) for (const key of Object.keys(select)) {
|
|
557
|
+
const fieldDef = modelDef.fields[key];
|
|
558
|
+
if (fieldDef && !fieldDef.relation) fields.add(key);
|
|
559
|
+
}
|
|
560
|
+
else for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
561
|
+
if (fieldDef.relation) continue;
|
|
562
|
+
if (omit?.[fieldName] === true) continue;
|
|
563
|
+
fields.add(fieldName);
|
|
564
|
+
}
|
|
565
|
+
return fields;
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Build the inner Zod schema for a relation field, optionally with nested
|
|
569
|
+
* query options. Does NOT apply cardinality/optional wrappers — the caller
|
|
570
|
+
* does that.
|
|
571
|
+
*/
|
|
572
|
+
makeRelationFieldSchema(fieldDef, subOptions) {
|
|
573
|
+
const relatedModelName = fieldDef.type;
|
|
574
|
+
if (subOptions) return this.makeModelSchema(relatedModelName, subOptions);
|
|
575
|
+
return z$1.lazy(() => this.makeModelSchema(relatedModelName));
|
|
576
|
+
}
|
|
577
|
+
makeScalarFieldSchema(fieldDef) {
|
|
578
|
+
const { type, attributes } = fieldDef;
|
|
579
|
+
if (this.schema.getEnum(type)) return this.applyCardinality(this.makeEnumSchema(type), fieldDef);
|
|
580
|
+
if (this.schema.getTypeDef(type)) return this.applyCardinality(this.makeTypeSchema(type), fieldDef);
|
|
581
|
+
let base;
|
|
582
|
+
switch (type) {
|
|
583
|
+
case "String":
|
|
584
|
+
base = addStringValidation(z$1.string(), attributes);
|
|
585
|
+
break;
|
|
586
|
+
case "Int":
|
|
587
|
+
base = addNumberValidation(z$1.number().int(), attributes);
|
|
588
|
+
break;
|
|
589
|
+
case "Float":
|
|
590
|
+
base = addNumberValidation(z$1.number(), attributes);
|
|
591
|
+
break;
|
|
592
|
+
case "Boolean":
|
|
593
|
+
base = z$1.boolean();
|
|
594
|
+
break;
|
|
595
|
+
case "BigInt":
|
|
596
|
+
base = addBigIntValidation(z$1.bigint(), attributes);
|
|
597
|
+
break;
|
|
598
|
+
case "Decimal":
|
|
599
|
+
base = z$1.union([
|
|
600
|
+
addNumberValidation(z$1.number(), attributes),
|
|
601
|
+
addDecimalValidation(z$1.string(), attributes, true),
|
|
602
|
+
addDecimalValidation(z$1.instanceof(Decimal), attributes, true)
|
|
603
|
+
]);
|
|
604
|
+
break;
|
|
605
|
+
case "DateTime":
|
|
606
|
+
base = z$1.union([z$1.date(), z$1.iso.datetime()]);
|
|
607
|
+
break;
|
|
608
|
+
case "Bytes":
|
|
609
|
+
base = z$1.instanceof(Uint8Array);
|
|
610
|
+
break;
|
|
611
|
+
case "Json":
|
|
612
|
+
base = this.makeJsonSchema();
|
|
613
|
+
break;
|
|
614
|
+
case "Unsupported":
|
|
615
|
+
base = z$1.unknown();
|
|
616
|
+
break;
|
|
617
|
+
default: throw new SchemaFactoryError(`Unsupported field type: ${type}`);
|
|
618
|
+
}
|
|
619
|
+
return this.applyCardinality(base, fieldDef);
|
|
620
|
+
}
|
|
621
|
+
makeJsonSchema() {
|
|
622
|
+
return z$1.union([
|
|
623
|
+
z$1.string(),
|
|
624
|
+
z$1.number(),
|
|
625
|
+
z$1.boolean(),
|
|
626
|
+
z$1.null(),
|
|
627
|
+
z$1.array(z$1.lazy(() => this.makeJsonSchema())),
|
|
628
|
+
z$1.object({}).catchall(z$1.lazy(() => this.makeJsonSchema()))
|
|
629
|
+
]);
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Wraps a schema with `.optional()` and copies any `description` from its
|
|
633
|
+
* metadata onto the resulting `ZodOptional`, so that callers inspecting
|
|
634
|
+
* `.meta()?.description` on shape fields still find the value after
|
|
635
|
+
* optionality has been applied.
|
|
636
|
+
*/
|
|
637
|
+
wrapOptionalPreservingMeta(schema) {
|
|
638
|
+
const optional = schema.optional();
|
|
639
|
+
const description = schema.meta()?.description;
|
|
640
|
+
if (description) return optional.meta({ description });
|
|
641
|
+
return optional;
|
|
642
|
+
}
|
|
643
|
+
applyCardinality(schema, fieldDef) {
|
|
644
|
+
let result = schema;
|
|
645
|
+
if (fieldDef.array) result = result.array();
|
|
646
|
+
if (fieldDef.optional) result = result.nullable().optional();
|
|
647
|
+
return result;
|
|
648
|
+
}
|
|
649
|
+
makeTypeSchema(type) {
|
|
650
|
+
const typeDef = this.schema.requireTypeDef(type);
|
|
651
|
+
const fields = {};
|
|
652
|
+
for (const [fieldName, fieldDef] of Object.entries(typeDef.fields)) fields[fieldName] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
653
|
+
const shape = z$1.strictObject(fields);
|
|
654
|
+
return this.applyDescription(addCustomValidation(shape, typeDef.attributes), typeDef.attributes);
|
|
655
|
+
}
|
|
656
|
+
makeEnumSchema(_enum) {
|
|
657
|
+
const enumDef = this.schema.requireEnum(_enum);
|
|
658
|
+
const schema = z$1.enum(Object.keys(enumDef.values));
|
|
659
|
+
return this.applyDescription(schema, enumDef.attributes);
|
|
660
|
+
}
|
|
661
|
+
getMetaDescription(attributes) {
|
|
662
|
+
if (!attributes) return void 0;
|
|
663
|
+
for (const attr of attributes) {
|
|
664
|
+
if (attr.name !== "@meta" && attr.name !== "@@meta") continue;
|
|
665
|
+
const nameExpr = attr.args?.[0]?.value;
|
|
666
|
+
if (!nameExpr || ExpressionUtils.getLiteralValue(nameExpr) !== "description") continue;
|
|
667
|
+
const valueExpr = attr.args?.[1]?.value;
|
|
668
|
+
if (valueExpr) return ExpressionUtils.getLiteralValue(valueExpr);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
applyDescription(schema, attributes) {
|
|
672
|
+
const description = this.getMetaDescription(attributes);
|
|
673
|
+
if (description) return schema.meta({ description });
|
|
674
|
+
return schema;
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
//#endregion
|
|
678
|
+
export { utils_exports as ZodUtils, createSchemaFactory };
|
|
679
|
+
|
|
680
|
+
//# sourceMappingURL=index.mjs.map
|