@zenstackhq/zod 3.4.0-beta.4 → 3.4.0
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/index.cjs +646 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +73 -7
- package/dist/index.d.ts +73 -7
- package/dist/index.js +637 -18
- package/dist/index.js.map +1 -1
- package/package.json +15 -7
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
9
|
var __export = (target, all) => {
|
|
@@ -16,39 +18,665 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
18
|
}
|
|
17
19
|
return to;
|
|
18
20
|
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
19
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
30
|
|
|
21
31
|
// src/index.ts
|
|
22
32
|
var src_exports = {};
|
|
23
33
|
__export(src_exports, {
|
|
24
|
-
|
|
34
|
+
ZodUtils: () => utils_exports,
|
|
35
|
+
createSchemaFactory: () => createSchemaFactory
|
|
25
36
|
});
|
|
26
37
|
module.exports = __toCommonJS(src_exports);
|
|
27
|
-
|
|
38
|
+
|
|
39
|
+
// src/factory.ts
|
|
40
|
+
var import_schema2 = require("@zenstackhq/schema");
|
|
41
|
+
var import_decimal2 = __toESM(require("decimal.js"), 1);
|
|
42
|
+
var import_zod2 = __toESM(require("zod"), 1);
|
|
43
|
+
|
|
44
|
+
// src/error.ts
|
|
45
|
+
var SchemaFactoryError = class extends Error {
|
|
46
|
+
static {
|
|
47
|
+
__name(this, "SchemaFactoryError");
|
|
48
|
+
}
|
|
49
|
+
constructor(message) {
|
|
50
|
+
super(message);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/utils.ts
|
|
55
|
+
var utils_exports = {};
|
|
56
|
+
__export(utils_exports, {
|
|
57
|
+
addBigIntValidation: () => addBigIntValidation,
|
|
58
|
+
addCustomValidation: () => addCustomValidation,
|
|
59
|
+
addDecimalValidation: () => addDecimalValidation,
|
|
60
|
+
addListValidation: () => addListValidation,
|
|
61
|
+
addNumberValidation: () => addNumberValidation,
|
|
62
|
+
addStringValidation: () => addStringValidation
|
|
63
|
+
});
|
|
64
|
+
var import_schema = require("@zenstackhq/schema");
|
|
65
|
+
var import_decimal = __toESM(require("decimal.js"), 1);
|
|
28
66
|
var import_zod = require("zod");
|
|
29
|
-
function
|
|
30
|
-
|
|
67
|
+
function getArgValue(expr) {
|
|
68
|
+
if (!expr || !import_schema.ExpressionUtils.isLiteral(expr)) {
|
|
69
|
+
return void 0;
|
|
70
|
+
}
|
|
71
|
+
return expr.value;
|
|
72
|
+
}
|
|
73
|
+
__name(getArgValue, "getArgValue");
|
|
74
|
+
function addStringValidation(schema, attributes) {
|
|
75
|
+
if (!attributes || attributes.length === 0) {
|
|
76
|
+
return schema;
|
|
77
|
+
}
|
|
78
|
+
let result = schema;
|
|
79
|
+
for (const attr of attributes) {
|
|
80
|
+
switch (attr.name) {
|
|
81
|
+
case "@length": {
|
|
82
|
+
const min = getArgValue(attr.args?.[0]?.value);
|
|
83
|
+
if (min !== void 0) {
|
|
84
|
+
result = result.min(min);
|
|
85
|
+
}
|
|
86
|
+
const max = getArgValue(attr.args?.[1]?.value);
|
|
87
|
+
if (max !== void 0) {
|
|
88
|
+
result = result.max(max);
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
case "@startsWith": {
|
|
93
|
+
const value = getArgValue(attr.args?.[0]?.value);
|
|
94
|
+
if (value !== void 0) {
|
|
95
|
+
result = result.startsWith(value);
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case "@endsWith": {
|
|
100
|
+
const value = getArgValue(attr.args?.[0]?.value);
|
|
101
|
+
if (value !== void 0) {
|
|
102
|
+
result = result.endsWith(value);
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "@contains": {
|
|
107
|
+
const value = getArgValue(attr.args?.[0]?.value);
|
|
108
|
+
if (value !== void 0) {
|
|
109
|
+
result = result.includes(value);
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "@regex": {
|
|
114
|
+
const pattern = getArgValue(attr.args?.[0]?.value);
|
|
115
|
+
if (pattern !== void 0) {
|
|
116
|
+
result = result.regex(new RegExp(pattern));
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case "@email":
|
|
121
|
+
result = result.email();
|
|
122
|
+
break;
|
|
123
|
+
case "@datetime":
|
|
124
|
+
result = result.datetime();
|
|
125
|
+
break;
|
|
126
|
+
case "@url":
|
|
127
|
+
result = result.url();
|
|
128
|
+
break;
|
|
129
|
+
case "@trim":
|
|
130
|
+
result = result.trim();
|
|
131
|
+
break;
|
|
132
|
+
case "@lower":
|
|
133
|
+
result = result.toLowerCase();
|
|
134
|
+
break;
|
|
135
|
+
case "@upper":
|
|
136
|
+
result = result.toUpperCase();
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
__name(addStringValidation, "addStringValidation");
|
|
143
|
+
function addNumberValidation(schema, attributes) {
|
|
144
|
+
if (!attributes || attributes.length === 0) {
|
|
145
|
+
return schema;
|
|
146
|
+
}
|
|
147
|
+
let result = schema;
|
|
148
|
+
for (const attr of attributes) {
|
|
149
|
+
const val = getArgValue(attr.args?.[0]?.value);
|
|
150
|
+
if (val === void 0) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
switch (attr.name) {
|
|
154
|
+
case "@gt":
|
|
155
|
+
result = result.gt(val);
|
|
156
|
+
break;
|
|
157
|
+
case "@gte":
|
|
158
|
+
result = result.gte(val);
|
|
159
|
+
break;
|
|
160
|
+
case "@lt":
|
|
161
|
+
result = result.lt(val);
|
|
162
|
+
break;
|
|
163
|
+
case "@lte":
|
|
164
|
+
result = result.lte(val);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return result;
|
|
31
169
|
}
|
|
32
|
-
__name(
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
throw new Error(`Model ${model} not found in schema`);
|
|
170
|
+
__name(addNumberValidation, "addNumberValidation");
|
|
171
|
+
function addBigIntValidation(schema, attributes) {
|
|
172
|
+
if (!attributes || attributes.length === 0) {
|
|
173
|
+
return schema;
|
|
37
174
|
}
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
175
|
+
let result = schema;
|
|
176
|
+
for (const attr of attributes) {
|
|
177
|
+
const val = getArgValue(attr.args?.[0]?.value);
|
|
178
|
+
if (val === void 0) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
switch (attr.name) {
|
|
182
|
+
case "@gt":
|
|
183
|
+
result = result.gt(BigInt(val));
|
|
184
|
+
break;
|
|
185
|
+
case "@gte":
|
|
186
|
+
result = result.gte(BigInt(val));
|
|
187
|
+
break;
|
|
188
|
+
case "@lt":
|
|
189
|
+
result = result.lt(BigInt(val));
|
|
190
|
+
break;
|
|
191
|
+
case "@lte":
|
|
192
|
+
result = result.lte(BigInt(val));
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
42
195
|
}
|
|
43
196
|
return result;
|
|
44
197
|
}
|
|
45
|
-
__name(
|
|
46
|
-
function
|
|
47
|
-
|
|
198
|
+
__name(addBigIntValidation, "addBigIntValidation");
|
|
199
|
+
function addDecimalValidation(schema, attributes, addExtraValidation) {
|
|
200
|
+
let result = schema;
|
|
201
|
+
if (schema instanceof import_zod.z.ZodString) {
|
|
202
|
+
result = schema.superRefine((v, ctx) => {
|
|
203
|
+
try {
|
|
204
|
+
new import_decimal.default(v);
|
|
205
|
+
} catch (err) {
|
|
206
|
+
ctx.addIssue({
|
|
207
|
+
code: "custom",
|
|
208
|
+
message: `Invalid decimal: ${err}`
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}).transform((val) => new import_decimal.default(val));
|
|
212
|
+
}
|
|
213
|
+
function refine(schema2, op, value) {
|
|
214
|
+
return schema2.superRefine((v, ctx) => {
|
|
215
|
+
const base = import_zod.z.number();
|
|
216
|
+
const { error } = base[op](value).safeParse(v.toNumber());
|
|
217
|
+
error?.issues.forEach((issue) => {
|
|
218
|
+
if (op === "gt" || op === "gte") {
|
|
219
|
+
ctx.addIssue({
|
|
220
|
+
code: "too_small",
|
|
221
|
+
origin: "number",
|
|
222
|
+
minimum: value,
|
|
223
|
+
type: "decimal",
|
|
224
|
+
inclusive: op === "gte",
|
|
225
|
+
message: issue.message
|
|
226
|
+
});
|
|
227
|
+
} else {
|
|
228
|
+
ctx.addIssue({
|
|
229
|
+
code: "too_big",
|
|
230
|
+
origin: "number",
|
|
231
|
+
maximum: value,
|
|
232
|
+
type: "decimal",
|
|
233
|
+
inclusive: op === "lte",
|
|
234
|
+
message: issue.message
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
__name(refine, "refine");
|
|
241
|
+
if (attributes && addExtraValidation) {
|
|
242
|
+
for (const attr of attributes) {
|
|
243
|
+
const val = getArgValue(attr.args?.[0]?.value);
|
|
244
|
+
if (val === void 0) {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
switch (attr.name) {
|
|
248
|
+
case "@gt":
|
|
249
|
+
result = refine(result, "gt", val);
|
|
250
|
+
break;
|
|
251
|
+
case "@gte":
|
|
252
|
+
result = refine(result, "gte", val);
|
|
253
|
+
break;
|
|
254
|
+
case "@lt":
|
|
255
|
+
result = refine(result, "lt", val);
|
|
256
|
+
break;
|
|
257
|
+
case "@lte":
|
|
258
|
+
result = refine(result, "lte", val);
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return result;
|
|
48
264
|
}
|
|
49
|
-
__name(
|
|
265
|
+
__name(addDecimalValidation, "addDecimalValidation");
|
|
266
|
+
function addListValidation(schema, attributes) {
|
|
267
|
+
if (!attributes || attributes.length === 0) {
|
|
268
|
+
return schema;
|
|
269
|
+
}
|
|
270
|
+
let result = schema;
|
|
271
|
+
for (const attr of attributes) {
|
|
272
|
+
if (attr.name === "@length") {
|
|
273
|
+
const min = getArgValue(attr.args?.[0]?.value);
|
|
274
|
+
if (min !== void 0) {
|
|
275
|
+
result = result.min(min);
|
|
276
|
+
}
|
|
277
|
+
const max = getArgValue(attr.args?.[1]?.value);
|
|
278
|
+
if (max !== void 0) {
|
|
279
|
+
result = result.max(max);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return result;
|
|
284
|
+
}
|
|
285
|
+
__name(addListValidation, "addListValidation");
|
|
286
|
+
function addCustomValidation(schema, attributes) {
|
|
287
|
+
const attrs = attributes?.filter((a) => a.name === "@@validate");
|
|
288
|
+
if (!attrs || attrs.length === 0) {
|
|
289
|
+
return schema;
|
|
290
|
+
}
|
|
291
|
+
let result = schema;
|
|
292
|
+
for (const attr of attrs) {
|
|
293
|
+
const expr = attr.args?.[0]?.value;
|
|
294
|
+
if (!expr) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
const message = getArgValue(attr.args?.[1]?.value);
|
|
298
|
+
const pathExpr = attr.args?.[2]?.value;
|
|
299
|
+
let path = void 0;
|
|
300
|
+
if (pathExpr && import_schema.ExpressionUtils.isArray(pathExpr)) {
|
|
301
|
+
path = pathExpr.items.map((e) => import_schema.ExpressionUtils.getLiteralValue(e));
|
|
302
|
+
}
|
|
303
|
+
result = applyValidation(result, expr, message, path);
|
|
304
|
+
}
|
|
305
|
+
return result;
|
|
306
|
+
}
|
|
307
|
+
__name(addCustomValidation, "addCustomValidation");
|
|
308
|
+
function applyValidation(schema, expr, message, path) {
|
|
309
|
+
const options = {};
|
|
310
|
+
if (message) {
|
|
311
|
+
options.error = message;
|
|
312
|
+
}
|
|
313
|
+
if (path) {
|
|
314
|
+
options.path = path;
|
|
315
|
+
}
|
|
316
|
+
return schema.refine((data) => Boolean(evalExpression(data, expr)), options);
|
|
317
|
+
}
|
|
318
|
+
__name(applyValidation, "applyValidation");
|
|
319
|
+
function evalExpression(data, expr) {
|
|
320
|
+
switch (expr.kind) {
|
|
321
|
+
case "literal":
|
|
322
|
+
return expr.value;
|
|
323
|
+
case "array":
|
|
324
|
+
return expr.items.map((item) => evalExpression(data, item));
|
|
325
|
+
case "field":
|
|
326
|
+
return evalField(data, expr);
|
|
327
|
+
case "member":
|
|
328
|
+
return evalMember(data, expr);
|
|
329
|
+
case "unary":
|
|
330
|
+
return evalUnary(data, expr);
|
|
331
|
+
case "binary":
|
|
332
|
+
return evalBinary(data, expr);
|
|
333
|
+
case "call":
|
|
334
|
+
return evalCall(data, expr);
|
|
335
|
+
case "this":
|
|
336
|
+
return data ?? null;
|
|
337
|
+
case "null":
|
|
338
|
+
return null;
|
|
339
|
+
case "binding":
|
|
340
|
+
throw new SchemaFactoryError("Binding expression is not supported in validation expressions");
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
__name(evalExpression, "evalExpression");
|
|
344
|
+
function evalField(data, e) {
|
|
345
|
+
return data?.[e.field] ?? null;
|
|
346
|
+
}
|
|
347
|
+
__name(evalField, "evalField");
|
|
348
|
+
function evalUnary(data, expr) {
|
|
349
|
+
const operand = evalExpression(data, expr.operand);
|
|
350
|
+
switch (expr.op) {
|
|
351
|
+
case "!":
|
|
352
|
+
return !operand;
|
|
353
|
+
default:
|
|
354
|
+
throw new SchemaFactoryError(`Unsupported unary operator: ${expr.op}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
__name(evalUnary, "evalUnary");
|
|
358
|
+
function evalBinary(data, expr) {
|
|
359
|
+
const left = evalExpression(data, expr.left);
|
|
360
|
+
const right = evalExpression(data, expr.right);
|
|
361
|
+
switch (expr.op) {
|
|
362
|
+
case "&&":
|
|
363
|
+
return Boolean(left) && Boolean(right);
|
|
364
|
+
case "||":
|
|
365
|
+
return Boolean(left) || Boolean(right);
|
|
366
|
+
case "==":
|
|
367
|
+
return left == right;
|
|
368
|
+
case "!=":
|
|
369
|
+
return left != right;
|
|
370
|
+
case "<":
|
|
371
|
+
return left < right;
|
|
372
|
+
case "<=":
|
|
373
|
+
return left <= right;
|
|
374
|
+
case ">":
|
|
375
|
+
return left > right;
|
|
376
|
+
case ">=":
|
|
377
|
+
return left >= right;
|
|
378
|
+
case "?":
|
|
379
|
+
if (!Array.isArray(left)) {
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
return left.some((item) => item === right);
|
|
383
|
+
case "!":
|
|
384
|
+
if (!Array.isArray(left)) {
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
return left.every((item) => item === right);
|
|
388
|
+
case "^":
|
|
389
|
+
if (!Array.isArray(left)) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
return !left.some((item) => item === right);
|
|
393
|
+
case "in":
|
|
394
|
+
if (!Array.isArray(right)) {
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
return right.includes(left);
|
|
398
|
+
default:
|
|
399
|
+
throw new SchemaFactoryError(`Unsupported binary operator: ${expr.op}`);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
__name(evalBinary, "evalBinary");
|
|
403
|
+
function evalMember(data, expr) {
|
|
404
|
+
let result = evalExpression(data, expr.receiver);
|
|
405
|
+
for (const member of expr.members) {
|
|
406
|
+
if (!result || typeof result !== "object") {
|
|
407
|
+
return void 0;
|
|
408
|
+
}
|
|
409
|
+
result = result[member];
|
|
410
|
+
}
|
|
411
|
+
return result ?? null;
|
|
412
|
+
}
|
|
413
|
+
__name(evalMember, "evalMember");
|
|
414
|
+
function evalCall(data, expr) {
|
|
415
|
+
const f = expr.function;
|
|
416
|
+
const fieldArg = expr.args?.[0] ? evalExpression(data, expr.args[0]) : void 0;
|
|
417
|
+
switch (f) {
|
|
418
|
+
// string functions
|
|
419
|
+
case "length": {
|
|
420
|
+
if (fieldArg === void 0 || fieldArg === null) {
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
invariant(typeof fieldArg === "string" || Array.isArray(fieldArg), `"${f}" first argument must be a string or a list`);
|
|
424
|
+
return fieldArg.length;
|
|
425
|
+
}
|
|
426
|
+
case "startsWith":
|
|
427
|
+
case "endsWith":
|
|
428
|
+
case "contains": {
|
|
429
|
+
if (fieldArg === void 0 || fieldArg === null) {
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
invariant(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
433
|
+
invariant(expr.args?.[1], `"${f}" requires a search argument`);
|
|
434
|
+
const search = getArgValue(expr.args?.[1]);
|
|
435
|
+
const caseInsensitive = getArgValue(expr.args?.[2]) ?? false;
|
|
436
|
+
const applyStringOp = /* @__PURE__ */ __name((x, y) => {
|
|
437
|
+
switch (f) {
|
|
438
|
+
case "startsWith":
|
|
439
|
+
return x.startsWith(y);
|
|
440
|
+
case "endsWith":
|
|
441
|
+
return x.endsWith(y);
|
|
442
|
+
case "contains":
|
|
443
|
+
return x.includes(y);
|
|
444
|
+
}
|
|
445
|
+
}, "applyStringOp");
|
|
446
|
+
return caseInsensitive ? applyStringOp(fieldArg.toLowerCase(), search.toLowerCase()) : applyStringOp(fieldArg, search);
|
|
447
|
+
}
|
|
448
|
+
case "regex": {
|
|
449
|
+
if (fieldArg === void 0 || fieldArg === null) {
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
invariant(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
453
|
+
const pattern = getArgValue(expr.args?.[1]);
|
|
454
|
+
invariant(pattern !== void 0, `"${f}" requires a pattern argument`);
|
|
455
|
+
return new RegExp(pattern).test(fieldArg);
|
|
456
|
+
}
|
|
457
|
+
case "isEmail":
|
|
458
|
+
case "isUrl":
|
|
459
|
+
case "isDateTime": {
|
|
460
|
+
if (fieldArg === void 0 || fieldArg === null) {
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
invariant(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
464
|
+
const fn = f === "isEmail" ? "email" : f === "isUrl" ? "url" : "datetime";
|
|
465
|
+
return import_zod.z.string()[fn]().safeParse(fieldArg).success;
|
|
466
|
+
}
|
|
467
|
+
// list functions
|
|
468
|
+
case "has":
|
|
469
|
+
case "hasEvery":
|
|
470
|
+
case "hasSome": {
|
|
471
|
+
invariant(expr.args?.[1], `${f} requires a search argument`);
|
|
472
|
+
if (fieldArg === void 0 || fieldArg === null) {
|
|
473
|
+
return false;
|
|
474
|
+
}
|
|
475
|
+
invariant(Array.isArray(fieldArg), `"${f}" first argument must be an array field`);
|
|
476
|
+
const search = evalExpression(data, expr.args?.[1]);
|
|
477
|
+
if (f === "has") {
|
|
478
|
+
return fieldArg.some((item) => item === search);
|
|
479
|
+
} else if (f === "hasEvery") {
|
|
480
|
+
invariant(Array.isArray(search), "hasEvery second argument must be an array");
|
|
481
|
+
return search.every((v) => fieldArg.some((item) => item === v));
|
|
482
|
+
} else {
|
|
483
|
+
invariant(Array.isArray(search), "hasSome second argument must be an array");
|
|
484
|
+
return search.some((v) => fieldArg.some((item) => item === v));
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
case "isEmpty": {
|
|
488
|
+
if (fieldArg === void 0 || fieldArg === null) {
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
invariant(Array.isArray(fieldArg), `"${f}" first argument must be an array field`);
|
|
492
|
+
return fieldArg.length === 0;
|
|
493
|
+
}
|
|
494
|
+
default:
|
|
495
|
+
throw new SchemaFactoryError(`Unsupported function "${f}"`);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
__name(evalCall, "evalCall");
|
|
499
|
+
function invariant(condition, message) {
|
|
500
|
+
if (!condition) {
|
|
501
|
+
throw new SchemaFactoryError(message ?? "Invariant failed");
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
__name(invariant, "invariant");
|
|
505
|
+
|
|
506
|
+
// src/factory.ts
|
|
507
|
+
function createSchemaFactory(schema) {
|
|
508
|
+
return new SchemaFactory(schema);
|
|
509
|
+
}
|
|
510
|
+
__name(createSchemaFactory, "createSchemaFactory");
|
|
511
|
+
var SchemaFactory = class SchemaFactory2 {
|
|
512
|
+
static {
|
|
513
|
+
__name(this, "SchemaFactory");
|
|
514
|
+
}
|
|
515
|
+
schema;
|
|
516
|
+
constructor(_schema) {
|
|
517
|
+
this.schema = new import_schema2.SchemaAccessor(_schema);
|
|
518
|
+
}
|
|
519
|
+
makeModelSchema(model) {
|
|
520
|
+
const modelDef = this.schema.requireModel(model);
|
|
521
|
+
const fields = {};
|
|
522
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
523
|
+
if (fieldDef.relation) {
|
|
524
|
+
const relatedModelName = fieldDef.type;
|
|
525
|
+
const lazySchema = import_zod2.default.lazy(() => this.makeModelSchema(relatedModelName));
|
|
526
|
+
fields[fieldName] = this.applyDescription(this.applyCardinality(lazySchema, fieldDef).optional(), fieldDef.attributes);
|
|
527
|
+
} else {
|
|
528
|
+
fields[fieldName] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const shape = import_zod2.default.strictObject(fields);
|
|
532
|
+
return this.applyDescription(addCustomValidation(shape, modelDef.attributes), modelDef.attributes);
|
|
533
|
+
}
|
|
534
|
+
makeModelCreateSchema(model) {
|
|
535
|
+
const modelDef = this.schema.requireModel(model);
|
|
536
|
+
const fields = {};
|
|
537
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
538
|
+
if (fieldDef.relation || fieldDef.computed || fieldDef.isDiscriminator) {
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
let fieldSchema = this.makeScalarFieldSchema(fieldDef);
|
|
542
|
+
if (fieldDef.optional || fieldDef.default !== void 0 || fieldDef.updatedAt) {
|
|
543
|
+
fieldSchema = fieldSchema.optional();
|
|
544
|
+
}
|
|
545
|
+
fields[fieldName] = this.applyDescription(fieldSchema, fieldDef.attributes);
|
|
546
|
+
}
|
|
547
|
+
const shape = import_zod2.default.strictObject(fields);
|
|
548
|
+
return this.applyDescription(addCustomValidation(shape, modelDef.attributes), modelDef.attributes);
|
|
549
|
+
}
|
|
550
|
+
makeModelUpdateSchema(model) {
|
|
551
|
+
const modelDef = this.schema.requireModel(model);
|
|
552
|
+
const fields = {};
|
|
553
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
554
|
+
if (fieldDef.relation || fieldDef.computed || fieldDef.isDiscriminator) {
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
let fieldSchema = this.makeScalarFieldSchema(fieldDef);
|
|
558
|
+
fieldSchema = fieldSchema.optional();
|
|
559
|
+
fields[fieldName] = this.applyDescription(fieldSchema, fieldDef.attributes);
|
|
560
|
+
}
|
|
561
|
+
const shape = import_zod2.default.strictObject(fields);
|
|
562
|
+
return this.applyDescription(addCustomValidation(shape, modelDef.attributes), modelDef.attributes);
|
|
563
|
+
}
|
|
564
|
+
makeScalarFieldSchema(fieldDef) {
|
|
565
|
+
const { type, attributes } = fieldDef;
|
|
566
|
+
const enumDef = this.schema.getEnum(type);
|
|
567
|
+
if (enumDef) {
|
|
568
|
+
return this.applyCardinality(this.makeEnumSchema(type), fieldDef);
|
|
569
|
+
}
|
|
570
|
+
const typedefDef = this.schema.getTypeDef(type);
|
|
571
|
+
if (typedefDef) {
|
|
572
|
+
return this.applyCardinality(this.makeTypeSchema(type), fieldDef);
|
|
573
|
+
}
|
|
574
|
+
let base;
|
|
575
|
+
switch (type) {
|
|
576
|
+
case "String":
|
|
577
|
+
base = addStringValidation(import_zod2.default.string(), attributes);
|
|
578
|
+
break;
|
|
579
|
+
case "Int":
|
|
580
|
+
base = addNumberValidation(import_zod2.default.number().int(), attributes);
|
|
581
|
+
break;
|
|
582
|
+
case "Float":
|
|
583
|
+
base = addNumberValidation(import_zod2.default.number(), attributes);
|
|
584
|
+
break;
|
|
585
|
+
case "Boolean":
|
|
586
|
+
base = import_zod2.default.boolean();
|
|
587
|
+
break;
|
|
588
|
+
case "BigInt":
|
|
589
|
+
base = addBigIntValidation(import_zod2.default.bigint(), attributes);
|
|
590
|
+
break;
|
|
591
|
+
case "Decimal":
|
|
592
|
+
base = import_zod2.default.union([
|
|
593
|
+
addNumberValidation(import_zod2.default.number(), attributes),
|
|
594
|
+
addDecimalValidation(import_zod2.default.string(), attributes, true),
|
|
595
|
+
addDecimalValidation(import_zod2.default.instanceof(import_decimal2.default), attributes, true)
|
|
596
|
+
]);
|
|
597
|
+
break;
|
|
598
|
+
case "DateTime":
|
|
599
|
+
base = import_zod2.default.union([
|
|
600
|
+
import_zod2.default.date(),
|
|
601
|
+
import_zod2.default.iso.datetime()
|
|
602
|
+
]);
|
|
603
|
+
break;
|
|
604
|
+
case "Bytes":
|
|
605
|
+
base = import_zod2.default.instanceof(Uint8Array);
|
|
606
|
+
break;
|
|
607
|
+
case "Json":
|
|
608
|
+
base = this.makeJsonSchema();
|
|
609
|
+
break;
|
|
610
|
+
case "Unsupported":
|
|
611
|
+
base = import_zod2.default.unknown();
|
|
612
|
+
break;
|
|
613
|
+
default: {
|
|
614
|
+
const _exhaustive = type;
|
|
615
|
+
throw new SchemaFactoryError(`Unsupported field type: ${_exhaustive}`);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return this.applyCardinality(base, fieldDef);
|
|
619
|
+
}
|
|
620
|
+
makeJsonSchema() {
|
|
621
|
+
return import_zod2.default.union([
|
|
622
|
+
import_zod2.default.string(),
|
|
623
|
+
import_zod2.default.number(),
|
|
624
|
+
import_zod2.default.boolean(),
|
|
625
|
+
import_zod2.default.null(),
|
|
626
|
+
import_zod2.default.array(import_zod2.default.lazy(() => this.makeJsonSchema())),
|
|
627
|
+
import_zod2.default.object({}).catchall(import_zod2.default.lazy(() => this.makeJsonSchema()))
|
|
628
|
+
]);
|
|
629
|
+
}
|
|
630
|
+
applyCardinality(schema, fieldDef) {
|
|
631
|
+
let result = schema;
|
|
632
|
+
if (fieldDef.array) {
|
|
633
|
+
result = result.array();
|
|
634
|
+
}
|
|
635
|
+
if (fieldDef.optional) {
|
|
636
|
+
result = result.nullable().optional();
|
|
637
|
+
}
|
|
638
|
+
return result;
|
|
639
|
+
}
|
|
640
|
+
makeTypeSchema(type) {
|
|
641
|
+
const typeDef = this.schema.requireTypeDef(type);
|
|
642
|
+
const fields = {};
|
|
643
|
+
for (const [fieldName, fieldDef] of Object.entries(typeDef.fields)) {
|
|
644
|
+
fields[fieldName] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
645
|
+
}
|
|
646
|
+
const shape = import_zod2.default.strictObject(fields);
|
|
647
|
+
return this.applyDescription(addCustomValidation(shape, typeDef.attributes), typeDef.attributes);
|
|
648
|
+
}
|
|
649
|
+
makeEnumSchema(_enum) {
|
|
650
|
+
const enumDef = this.schema.requireEnum(_enum);
|
|
651
|
+
const schema = import_zod2.default.enum(Object.keys(enumDef.values));
|
|
652
|
+
return this.applyDescription(schema, enumDef.attributes);
|
|
653
|
+
}
|
|
654
|
+
getMetaDescription(attributes) {
|
|
655
|
+
if (!attributes) return void 0;
|
|
656
|
+
for (const attr of attributes) {
|
|
657
|
+
if (attr.name !== "@meta" && attr.name !== "@@meta") continue;
|
|
658
|
+
const nameExpr = attr.args?.[0]?.value;
|
|
659
|
+
if (!nameExpr || import_schema2.ExpressionUtils.getLiteralValue(nameExpr) !== "description") continue;
|
|
660
|
+
const valueExpr = attr.args?.[1]?.value;
|
|
661
|
+
if (valueExpr) {
|
|
662
|
+
return import_schema2.ExpressionUtils.getLiteralValue(valueExpr);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
return void 0;
|
|
666
|
+
}
|
|
667
|
+
applyDescription(schema, attributes) {
|
|
668
|
+
const description = this.getMetaDescription(attributes);
|
|
669
|
+
if (description) {
|
|
670
|
+
return schema.meta({
|
|
671
|
+
description
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
return schema;
|
|
675
|
+
}
|
|
676
|
+
};
|
|
50
677
|
// Annotate the CommonJS export names for ESM import in node:
|
|
51
678
|
0 && (module.exports = {
|
|
52
|
-
|
|
679
|
+
ZodUtils,
|
|
680
|
+
createSchemaFactory
|
|
53
681
|
});
|
|
54
682
|
//# sourceMappingURL=index.cjs.map
|